Python running out of memory... I think. Raspberry Pi 3

I am currently working on a project that adds a flashing effect to 2 RGB LED lights. basically I am calling the home assistant web api specifying a color, waiting 0.75 seconds, and calling the same endpoint with a different color. conditions in a python script determine whether this feature should be switched on or off. If this runs too long though, it seems that I am getting a memory dump (i think its that and my inspecting the kernel messages, it seems like home assistant. I have no clue about memory management so I guess this will be the time to learn.

Code I am using for the flashing effect. This runs in a loop over and over and over until a condition is met to break out of the loop:

data = '{"entity_id": "light.divider_light","rgb_color":[255,0,0], "brightness": 255}'
                response = requests.post(url='http://IP:8123/api/services/light/turn_on',
                                                data=data,
                                                headers={'Content-Type': 'application/json'})

                data = '{"entity_id": "light.corner_accent_light","rgb_color":[255,0,0], "brightness": 255}'
                response = requests.post(url='http://IP:8123/api/services/light/turn_on',
                                                data=data,
                                                headers={'Content-Type': 'application/json'})

                sleep(0.75)

                data = '{"entity_id": "light.divider_light","rgb_color":[0,0,255], "brightness": 255}'
                response = requests.post(url='http://IP:8123/api/services/light/turn_on',
                                                data=data,
                                                headers={'Content-Type': 'application/json'})

                data = '{"entity_id": "light.corner_accent_light","rgb_color":[0,0,255], "brightness": 255}'
                response = requests.post(url='http://IP:8123/api/services/light/turn_on',
                                                data=data,
                                                headers={'Content-Type': 'application/json'})

                sleep(0.75)

Message eventually thrown after which I have to hard reset everything:

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435513] Internal error: : 1 [#1] SMP ARM

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435672] Process hass (pid: 542, stack limit = 0xb5d2e210)

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435678] Stack: (0xb5d2fda0 to 0xb5d30000)

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435686] fda0: b66fe6c0 0000005f 0000005f b5e91f40 b5d2fdd4 b5d2fdc0 806118c4 8061177c

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435695] fdc0: b5e91fc4 b66fe6c0 b5d2fdec b5d2fdd8 806118e4 806118a0 b5e91fc4 b66fe6c0

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435703] fde0: b5d2fe5c b5d2fdf0 806775a8 806118d4 8045cddc 20000013 b5d2fe70 b9f74600

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435711] fe00: 0000012d b5d2fdf0 00000000 b5d2feb8 80c05d48 b5d2e000 00000000 b5e92400

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435719] fe20: 00000001 0003ffa1 80187248 00000000 b5d2fe64 b5d2feb8 ad941500 00000000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435727] fe40: 00000000 00000040 b5d2e000 00000000 b5d2fe8c b5d2fe60 806a3fcc 806770ec

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435735] fe60: 00000000 b5d2fe6c b5d2fe84 00000000 8028f6d8 00000000 ad941500 00000000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435743] fe80: b5d2fe9c b5d2fe90 806088a4 806a3f34 b5d2ff8c b5d2fea0 8060b090 80608888

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435751] fea0: b5d2fec0 b5d2feb0 fffffff7 00000001 013734f0 00040000 00000000 00000000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435758] fec0: 00000000 0000005f 0003ffa1 b5d2feb0 00000001 b9225c01 00000000 00000000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435766] fee0: b93daeac 00000000 b5d2ffa4 b5d2fef8 802bcf40 80271f18 b5d2ff14 b5d2ff08

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435774] ff00: 805d9bf8 805d98b4 b5d2ff5c b5d2ff18 8018ce38 805d9be0 00044a5c 00000000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435782] ff20: 361bb124 0001181f 0000050e 00000000 01f7955e 00000000 b5d2ff88 00000001

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435790] ff40: 00a49cc0 00000107 80108244 b5d2e000 b5d2ff84 00000001 00a49cc0 00000107

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435799] ff60: 80108244 7ecc7504 00000008 ffffffff 6e545e40 00000000 00000123 80108244

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435807] ff80: b5d2ffa4 b5d2ff90 8060b114 8060aff4 00000000 00000000 00000000 b5d2ffa8

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435815] ffa0: 801080c0 8060b0f8 ffffffff 6e545e40 00000018 013734f0 00040000 00000000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435823] ffc0: ffffffff 6e545e40 00000000 00000123 76fee4d0 00000000 00a49cc0 00040000

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435831] ffe0: 00000000 7ecc7370 76f82504 76f830c4 80000010 00000018 3a7fa861 3a7fac61

Message from syslogd@HA-RasPi at May 22 17:35:05 ...
 kernel:[ 1294.435935] Code: e2873020 17df2812 f593f000 e1931f9f (e0411002)
^CTraceback (most recent call last):
  File "LightsDaemon.py", line 42, in <module>
    headers={'Content-Type': 'application/json'})
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 362, in send
    timeout=timeout
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 516, in urlopen
    body=body, headers=headers)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 331, in _make_request
    httplib_response = conn.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1111, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 444, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 400, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/lib/python2.7/socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
KeyboardInterrupt

Any help how I can fix this will be highly appreciated.

Thanks

You could add this sensor to HA to monitor the memory use:

- platform: systemmonitor
  resources:
    - type: memory_use_percent
1 Like

Im going to give that a go. That would report the global system memory correct?

I’m pretty sure, yes.

I think this means it filled up its stack, rather than ran out of memory. It could be that it can’t process http message at that rate. Try increasing the delay between messages.

Its worth a try. What would you suggest would be a viable test? 1 second or am I being ignorantly optimistic? I’m a bit of layman at this. is there possibly a way to clear the stack?

I would go for something extreme to start with, to judge its behaviour. Even a Pi should be able to handle a message every 10s, but if the problem occurs again at the same rate, then it probably is not the cause.