-
Notifications
You must be signed in to change notification settings - Fork 1.1k
unix-ffi: Fixing behavior to avoid zombie threads. #1046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
klukonin
wants to merge
1
commit into
micropython:master
Choose a base branch
from
klukonin:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |
| write_ = libc.func("i", "write", "iPi") | ||
| close_ = libc.func("i", "close", "i") | ||
| dup_ = libc.func("i", "dup", "i") | ||
| dup2_ = libc.func("i", "dup2", "ii") | ||
| access_ = libc.func("i", "access", "si") | ||
| fork_ = libc.func("i", "fork", "") | ||
| pipe_ = libc.func("i", "pipe", "p") | ||
|
|
@@ -207,6 +208,10 @@ def dup(fd): | |
| check_error(r) | ||
| return r | ||
|
|
||
| def dup2(oldfd, newfd): | ||
| r = dup2_(oldfd, newfd) | ||
| check_error(r) | ||
| return r | ||
|
|
||
| def access(path, mode): | ||
| return access_(path, mode) == 0 | ||
|
|
@@ -293,24 +298,53 @@ def urandom(n): | |
| with builtins.open("/dev/urandom", "rb") as f: | ||
| return f.read(n) | ||
|
|
||
| class _PopenStream: | ||
| def __init__(self, fd, pid, mode): | ||
| self._fd = open(f"/dev/fd/{fd}", mode) | ||
| self._pid = pid | ||
| self._closed = False | ||
| self._exitcode = None | ||
|
|
||
| def popen(cmd, mode="r"): | ||
| import builtins | ||
| def __enter__(self): | ||
| return self._fd | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| self.close() | ||
|
|
||
| i, o = pipe() | ||
| if mode[0] == "w": | ||
| i, o = o, i | ||
| def close(self): | ||
| if not self._closed: | ||
| try: | ||
| self._fd.close() | ||
| finally: | ||
| pid, status = waitpid(self._pid, 0) | ||
| self._exitcode = status | ||
| self._closed = True | ||
| return self._exitcode | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This 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. |
||
|
|
||
|
|
||
| def popen(cmd, mode='r'): | ||
| rfd, wfd = pipe() | ||
| pid = fork() | ||
| if not pid: | ||
| if mode[0] == "r": | ||
| close(1) | ||
|
|
||
| if pid == 0: | ||
| # --- CHILD --- | ||
| if mode[0] == 'r': | ||
| close(rfd) | ||
| dup2(wfd, 1) # connect pipe to stdout | ||
| close(wfd) | ||
| else: | ||
| close(0) | ||
| close(i) | ||
| dup(o) | ||
| close(o) | ||
| s = system(cmd) | ||
| _exit(s) | ||
| close(wfd) | ||
| dup2(rfd, 0) # connect pipe to stdin | ||
| close(rfd) | ||
|
|
||
| execvp("sh", ["sh", "-c", cmd]) | ||
| # If execvp SUCCEEDS, the code below is NEVER executed. | ||
| _exit(127) | ||
|
|
||
| # --- PARENT --- | ||
| if mode[0] == 'r': | ||
| close(wfd) | ||
| return _PopenStream(rfd, pid, mode) | ||
| else: | ||
| close(o) | ||
| return builtins.open(i, mode) | ||
| close(rfd) | ||
| return _PopenStream(wfd, pid, mode) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.openso that the returned object is a stream.