uWSGI lazy-apps and ThreadPool - multithreading

Running uWSGI with
$ cat /etc/uwsgi/uwsgi.cfg
[uwsgi]
callable = app
socket = /var/run/arivale-service/uwsgi.sock
chmod-socket = 666
pidfile = /var/run/arivale-service/uwsgi.pid
master = true
enable-threads = true
single-interpreter = true
thunder-lock
need-app
processes = 4
Without lazy-apps enabled, a request to the calling following endpoint hangs
import boto3
# ...irrelevant imports
from multiprocessing.dummy import Pool as ThreadPool
POOL = ThreadPool(6)
# ...irrelevant setup
def get_ecs_task_definitions(service_name):
ecs_service_name, _ = get_ecs_service_name_and_cluster(service_name)
def get_task_definition(task_definition_arn):
formatted_task_definition = {}
task_definition = ECS.describe_task_definition(taskDefinition=task_definition_arn)['taskDefinition']
# ...
# build formatted_task_definition from task_definition
# ...
return formatted_task_definition
task_definition_arns = ECS.list_task_definitions(
familyPrefix=ecs_service_name, status='ACTIVE')['taskDefinitionArns']
return POOL.map(get_task_definition, task_definition_arns)
#service.api('/api/services/<service_name>/ecs/task-definitions')
def get_task_definitions(service_name):
return {'service_name': service_name, 'task_definitions': get_ecs_task_definitions(service_name)}
NGINX is balancing uWSGI apps, and this is what it says in error.log (NGINX):
Jun 10 03:54:26 93e04e04e2cf nginx_error: 2017/06/10 03:54:26 [error] 49#49: *33 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.16.254.95, server: localhost, request: "GET /api/services/data-analysis-service/ecs/task-definitions HTTP/1.1",upstream: "uwsgi://unix:/var/run/arivale-service/uwsgi.sock", host: "devops-service.arivale.com"
Each request to the endpoint hangs a worker (below is the output of uwsgitop after 2 requests):
uwsgi-2.0.15 - Sat Jun 10 21:26:10 2017 - req: 0 - RPS: 0 - lq: 0 - tx: 0
node: localhost - cwd: /var/www/arivale-service/web - uid: 0 - gid: 0 - masterpid: 45
WID % PID REQ RPS EXC SIG STATUS AVG RSS VSZ TX ReSpwn HC RunT LastSpwn
1 0.0 74 0 0 0 0 busy 0ms 0 0 0 1 0 0 21:23:20
2 0.0 75 0 0 0 0 busy 0ms 0 0 0 1 0 0 21:23:20
3 0.0 76 0 0 0 0 idle 0ms 0 0 0 1 0 0 21:23:20
4 0.0 77 0 0 0 0 idle 0ms 0 0 0 1 0 0 21:23:20
Enabling lazy-apps fixes the issue. Does anyone know with certainty why?

This happened because uWSGI workers were unable to access the thread pool created in the master, see https://stackoverflow.com/a/39890941/2419669.
Using #postfork fixes this:
global THREAD_POOL = None
#postfork
def _make_thread_pool():
global THREAD_POOL
THREAD_POOL = ThreadPool(8)

Related

Why the python script process enter the sleep state and can't be woken up for execution?

This script is using grequests concurrent request url. And when it runs for 33 min, it goes to sleep permanently. And I don't no why. This script used to run well.
The main code is, :
urlList.append(grequests.get(url))
num = num + 1
if num = 50:
resList = grequests.map(urlList, exception_handler=err_handler)
num = 0
urlList = []
The process status:
ps aux | grep build.py
work 27647 3.8 0.0 972736 29476 ? Sl Jan27 33:26 python build.py
strace -p 27647
Process 27647 attached
clock_gettime(CLOCK_MONOTONIC, {1543747, 295274224}) = 0
clock_gettime(CLOCK_MONOTONIC, {1543747, 295337559}) = 0
epoll_wait(11, {}, 64, 59743) = 0
clock_gettime(CLOCK_MONOTONIC, {1543807, 97334455}) = 0
clock_gettime(CLOCK_MONOTONIC, {1543807, 97428028}) = 0
epoll_wait(11, {}, 64, 59743) = 0
clock_gettime(CLOCK_MONOTONIC, {1543866, 900111931}) = 0
clock_gettime(CLOCK_MONOTONIC, {1543866, 900158914}) = 0
env:
CentOS release 6.3
Python 2.7.13
grequests 0.6.0

How to set up optional parameter in LDP Layer using scapy

I am trying to build LDPHello packets, but I am not able to add optional parameter like IPv4 Transport layer using scapy.
I have done this:
ip = IP(src=src_ipv4, dst= '224.0.0.2', proto=17, ttl=1)
udp = UDP(sport=646, dport=646)
hello = LDPHello(params=[180,0,0,0,0,"2.2.2.2",0])
packet = ip / udp / hello
Which gives me output like:
###[ IP ]###
version = 4
ihl = 5
tos = 0x0
len = 54
id = 1
flags =
frag = 0
ttl = 1
proto = udp
chksum = 0x6c44
src = 10.110.99.2
dst = 224.0.0.2
\options \
###[ UDP ]###
sport = 646
dport = 646
len = 34
chksum = 0xa34c
###[ LDP ]###
version = 1
len = 22
id = 2.2.2.2
space = 0
###[ LDPHello ]###
u = 0
type = 256
len = 12
id = 0
params = [180, 0, 0]
By LDPHello Definition from scapy:
class LDPHello(_LDP_Packet):
name = "LDPHello"
fields_desc = [BitField("u", 0, 1),
BitField("type", 0x0100, 15),
ShortField("len", None),
IntField("id", 0),
CommonHelloTLVField("params", [180, 0, 0])]
After CommonHelloTLVField there is no field where we can add optional parameter like IPv4 transport Address.
While by RFC the total data structure is like:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|0| Hello (0x0100) | Message Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Message ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Common Hello Parameters TLV |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Optional Parameters |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
I don't know how to do get output like:Contains IPv4 Transport Layer

pure-python callback function was called with parameter b'', lossing the actual data in cython function

I try to call a callback function in cython cdef block, I can printf the actual data during cython function call, but when my callback function was called or I call a print(buffer) in cython cdef block, I got the (paramter/result) with b'',I try other demo, It's all works fine.
windows 10, python3.7, cython
cython block
cdef ikcp_output(self, char *buffer, int size):
print("size: {}".format(size)) this can work
for i in range(size):
print(buffer[i]) # this can also works
cdef char* o
o = <char*> malloc(sizeof(char) * size)
memcpy(o, buffer, size)
for i in range(size):
print(o[i]) # this can also works
print(o) # this output b''
self.output(o) # the callback function get a parameter b''
free(o)
start iter buffer
0
0
0
1
81
0
0
32
28
-50
-113
-50
0
0
0
0
0
0
0
0
49
50
49
51
50
49
51
49
51
49
50
10
start iter o
0
0
0
1
81
0
0
32
28
-50
-113
-50
0
0
0
0
0
0
0
0
49
50
49
51
50
49
51
49
51
49
50
10
32
b'' # this is what i got in callback function
client output
output data b'' to addr: ('127.0.0.1', 8888)
Calling either print(o) or self.func(o) involves converting o to a Python object, in this case a bytes object. This conversion assumes that the char* is a c string (i.e. is null terminated) since Cython has no other way to determine the length. However your buffer starts with 0, and so is read a 0-length string.
The correct solution is to do the conversion manually using PyBytes_FromStringAndSize. You pass this a char* and the length of the string (hence it doesn't assume that the first 0 is the end of the string). You can cimport it from cpython.bytes.

MCP23017 GPIO Interrupt Handling (RPi)

I have connected a MCP23017 I2C GPIO Expander chip to RPi. I use the this device driver and the mcp23017.dtbo overlay file.
I am trying to check the interrupt handling with the gpio pins. I did the following to connect the pins to interrupt(496 is the base which is GPA0 of MCP23017):
echo "496" > /sys/class/gpio/export
echo "both" > /sys/class/gpio/gpio496/edge
and then to trigger:
cat gpio496/value
But when i check /proc/interrupts it always shows 0 interrupts:
root#raspberrypi:/sys/class/gpio# cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
16: 0 0 0 0 bcm2836-timer 0 Edge arch_timer
17: 41827 52799 8260 86838 bcm2836-timer 1 Edge arch_timer
23: 67 0 0 0 ARMCTRL-level 1 Edge 3f00b880.mailbox
24: 248 0 0 0 ARMCTRL-level 2 Edge VCHIQ doorbell
46: 0 0 0 0 ARMCTRL-level 48 Edge bcm2708_fb dma
48: 4125 0 0 0 ARMCTRL-level 50 Edge DMA IRQ
50: 0 0 0 0 ARMCTRL-level 52 Edge DMA IRQ
62: 415706 0 0 0 ARMCTRL-level 64 Edge dwc_otg, dwc_otg_pcd, dwc_otg_hcd:usb1
79: 0 0 0 0 ARMCTRL-level 81 Edge 3f200000.gpio:bank0
80: 0 0 0 0 ARMCTRL-level 82 Edge 3f200000.gpio:bank1
83: 945 0 0 0 ARMCTRL-level 85 Edge 3f804000.i2c
86: 502 0 0 0 ARMCTRL-level 88 Edge mmc0
87: 5032 0 0 0 ARMCTRL-level 89 Edge uart-pl011
92: 6828 0 0 0 ARMCTRL-level 94 Edge mmc1
170: 0 0 0 0 pinctrl-bcm2835 4 Edge 1-0020, 1-0021, 1-0022, 1-0023
220: 0 0 0 0 gpio-mcp23xxx 0 Edge gpiolib
Also, when the overlay is loaded, the probe funtion is called and i get the following message:
genirq: irq 170 uses trigger mode 8; requested 2
I could not figure out the problem. I am not sure if there is something wrong in the overlay file. Below is the .dts overlay file:
// Definitions for MCP23017 Gpio Extender from Microchip Semiconductor
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
fragment#0 {
target = <&i2c1>;
__overlay__ {
status = "okay";
};
};
fragment#1 {
target = <&gpio>;
__overlay__ {
mcp23017_pins: mcp23017_pins {
brcm,pins = <4>;
brcm,function = <0>;
};
};
};
fragment#2 {
target = <&i2c1>;
__overlay__ {
#address-cells = <1>;
#size-cells = <0>;
mcp20:mcp23017#20 {
compatible = "microchip,mcp23017";
reg = <0x20>;
gpio-controller;
#gpio-cells = <2>;
interrupt-parent = <&gpio>;
interrupts = <4 2>;
interrupt-controller;
#interrupt-cells=<2>;
microchip,irq-mirror;
status = "okay";
};
};
};
__overrides__ {
gpiopin = <&mcp23017_pins>,"brcm,pins:0",
<&mcp20>,"interrupts:0";
addr = <&mcp20>,"reg:0";
};
};

Cent OS - sysctl.conf not working

The settings i have applied in my sysctl.conf are not working. Not on reboot or after applying it using sysctl -p
Below are my sysctl.conf settings. Can someone take a look and tell me what may be causing the issue?
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.ipv4.tcp_keepalive_time = 4
net.ipv4.tcp_keepalive_probes = 1
net.ipv4.tcp_keepalive_intvl = 2
net.ipv4.tcp_fin_timeout = 4
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rfc1337 = 1
net.ipv4.tcp_congestion_control= cubic
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.route.flush = 1
net.ipv4.tcp_orphan_retries = 0
net.core.netdev_max_backlog = 400000
net.core.optmem_max = 10000000
net.core.rmem_default = 10000000
net.core.rmem_max = 10000000
net.core.somaxconn = 100000
net.core.wmem_default = 10000000
net.core.wmem_max = 10000000
net.ipv4.conf.all.rp_filter = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_ecn = 0
net.ipv4.tcp_max_syn_backlog = 12000
net.ipv4.tcp_max_tw_buckets = 2000000
net.ipv4.tcp_mem = 10240 8738000 825829120
net.ipv4.tcp_rmem = 10240 8738000 825829120
net.ipv4.tcp_sack = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_wmem = 10240 8738000 825829120
net.netfilter.nf_conntrack_max = 768000
net.netfilter.nf_conntrack_generic_timeout = 4
net.netfilter.nf_conntrack_tcp_timeout_established = 4
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 4
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 4
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 4
net.netfilter.nf_conntrack_tcp_timeout_last_ack = 4
net.netfilter.nf_conntrack_tcp_timeout_syn_sent = 4
net.netfilter.nf_conntrack_tcp_timeout_syn_recv = 4
net.netfilter.nf_conntrack_tcp_timeout_close = 4

Resources