Skip to content

Conversation

@klukonin
Copy link

@klukonin klukonin commented Sep 25, 2025

This commit is fixing this issue:
#780
As discussed here https://github.com/orgs/micropython/discussions/13239

Now the pid of child thread will be stored and closed properly. Even with context manager.

@klukonin klukonin changed the title unix-ffi: Fixing behavior to avoid zombie threads unix-ffi: Fixing behavior to avoid zombie threads. Sep 25, 2025
This commit is fixing  this issue:
micropython#780

Now the pid of child thread will be stored and closed properly.
Even with context manager.

Signed-off-by: Kirill Lukonin (Evil Wireless Man) <[email protected]>
@hodan121
Copy link

bump up

@Josverl
Copy link
Contributor

Josverl commented Oct 20, 2025

It would be good to add a test for this functionality,
that also helps with maintaining stability over time.

Other tests for the ffi module live in the micropython repo under tests/ports/unix

you could take one of the other ffi_ tests as the base, and add example code to it.


class _PopenStream:
def __init__(self, fd, pid, mode):
self._fd = open(f"/dev/fd/{fd}", mode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use builtins.open so that the returned object is a stream.

pid, status = waitpid(self._pid, 0)
self._exitcode = status
self._closed = True
return self._exitcode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This _PopenStream object will need to implement read/write/etc operations, so that you can communicate with the process.

Eg prior to this patch it's possible to do:

import os
ls = os.popen("/bin/ls")
print(ls.read())
ls.close()

and that still needs to work.

@klukonin
Copy link
Author

klukonin commented Dec 2, 2025

I'll update the PR.
Thanks for the ideas, folks =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants