Python subprocess with snapcraft-preload causes memory leak

Hi, there!

I’m developing a snap that runs a python application using snapcraft-preload and am observing a memory increase whenever a subprocess is run.

Minimal example:

import subprocess
while True:
  subprocess.run(["/bin/ls"])

Running this via snap shell directly with python3 works as expected, while ps aux reports increasing rss and vsz when the same code is executed via bin/snapcraft-preload python3. Sample output:

$ while true; do ps aux | grep [4]2325; sleep 2; done
root       42325  0.4  0.6  17980 12772 pts/0    S+   10:10   0:00 python3
root       42325  0.4  0.6  17980 12772 pts/0    S+   10:10   0:00 python3
root       42325  0.4  0.6  17980 12772 pts/0    S+   10:10   0:00 python3
root       42325  0.3  0.6  17980 12772 pts/0    S+   10:10   0:00 python3
root       42325  0.7  0.7  18928 14092 pts/0    S+   10:10   0:00 python3
root       42325  1.1  0.8  20284 15308 pts/0    S+   10:10   0:00 python3
root       42325  1.4  0.8  21504 16520 pts/0    S+   10:10   0:00 python3
root       42325  1.7  0.9  22724 17708 pts/0    S+   10:10   0:00 python3
root       42325  2.1  0.9  23808 18840 pts/0    S+   10:10   0:00 python3
root       42325  2.0  0.9  23808 18840 pts/0    S+   10:10   0:00 python3
root       42325  1.9  0.9  23808 18840 pts/0    S+   10:10   0:00 python3
root       42325  1.8  0.9  23808 18840 pts/0    S+   10:10   0:00 python3
root       42325  1.8  0.9  23808 18840 pts/0    S+   10:10   0:00 python3
root       42325  1.7  0.9  23808 18840 pts/0    S+   10:10   0:00 python3

Stable memory usage before and after running the loop but continually increasing while it’s running.

Any idea what might cause this or how to avoid it? Any feedback is appreciated!

PS: normally I’d open an issue on GitHub but given the lack of responses there for several years, I figured I’d try here first.

1 Like