-
Notifications
You must be signed in to change notification settings - Fork 222
Stream tool output through BenchExec for truncation prevention #1170
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
Draft
t0hsumi
wants to merge
12
commits into
main
Choose a base branch
from
add-output-selector
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6526b5d
Add the pipe functionality to BaseExecutor class.
t0hsumi 3d02db1
Incorporate the resource cleanups in `wait_for_read()`
t0hsumi ed72686
Make `_stream_output_with_selector()`
t0hsumi a6fde54
Flush the output after finishing reading
t0hsumi 8e1044b
Pipe the output in `ContainerExecutor()`
t0hsumi dbdd8de
Delete non-blocking update to process stdout/stderr
t0hsumi 6c953c1
Disable pipe if the output file is not specified
t0hsumi 8a34a3e
Remove unused import
t0hsumi 65d4b7c
Enable to exit if there is no event
t0hsumi 7b3c030
Continue when `selector.select()` returns empty list
t0hsumi eab95e4
Add the explanation of the argments
t0hsumi 07d7036
Use pidfd_open to monitor whether the process terminates
t0hsumi 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
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
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
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.
If we call self._stream_output_with_selector() here and the tool does not send an EOF but is instead terminated by a signal, the procedure gets blocked in this function (more precisely, in selector.select() within self._stream_output_with_selector()), and runexec does not terminate. (See this pytest error case.)
To handle this properly, we need to process stdout and stderr while also checking for walltime limits and signals. In the implementation, we need to integrate self._stream_output_with_selector() with wait_for_child_and_forward_signals(). However, as far as I can tell, there are two possible solutions—neither of which seems ideal:
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.
Can you make select wait for termination of a process? I think that might be possible and should solve the problem.
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.
I’m trying to address the pytest error case. According to the selectors documentation, the latest code should detect when the events list is empty. An empty list indicates that "this method can return before any file object becomes ready or the timeout has elapsed if the current process receives a signal."
However, in the pytest error case, when the timeout limit is exceeded, a SIGKILL is sent to the grandchild process. Despite this, selector.select() does not return an empty list in response, which contradicts the expected behavior. I don’t yet understand the reason for this.
As far as I can tell from the documentation, there seems to be no other way within BenchExec to detect process termination using only the selectors module.
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.
I think the documentation talks about the case if the select()ing process receives a signal. But this is not our case.
I would assume that the pipe is closed if the only listening process terminates, and that this causes select() to return. Can you check if there are any other processes that still have the same pipe open, for example BenchExec itself? BenchExec should only have the read ends of the pipe open, and the benchmarked process only the write ends.
Another way could be to add a file descriptor that signals if the process ends. Maybe we can do this with a pidfd. Or the part of the code that calls wait could close the pipes, such that select() returns?
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.
Sorry for the late reply.
Thanks for pointing that out — I misunderstood. I've now updated the code to retry select() when it returns an empty list.
Which pipe do you mean? I assume you're referring to
proc.stdoutandproc.stderr, but I couldn't find any indication that BenchExec is reading from those.I’m now using
pidfd_open()together with theselectmodule. It works fine in my environment.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.
Yes, I was referring to
stdoutandstderr. These should be pipes, like those returned byos.pipe(). Whether something is reading from them is not important, the question is whether there exists any process that still has an open file descriptor that is referring to their write end (after opening a pipe consists of two file descriptors, which can be passed around and closed individually). I would think (hope) that as soon as the last process terminates that has the write end open, the pipe is closed andselect()should tell us.This is nice! I general I think that PID fds are great and eventually BenchExec will use them anyway. However, they are available only since Linux 5.1, and this is still somewhat new. I would prefer if we do not introduce a hard dependency. So if we find a solution without PID fs, it would be nicer. I am pretty sure there is such a way, because what we need seems like a highly common problem for subprocess spawning and many tools have needed this for a long time even before the introduction of PID fs.
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.
Alright, I’ll check which process is opening which file descriptors.