Repository navigation
libobs/util: Fix posix_spawn failed when read pipe needed on macOS - #12147
summershrimp wants to merge 1 commit into
Conversation
kkartaltepe
left a comment
There was a problem hiding this comment.
code is fine, the commit message should probably be something like avoiding double close.
RytoEX
left a comment
There was a problem hiding this comment.
Nits:
- libbos -> libobs
- Capitalize the first letter of the first word after the commit prefix (Sentence case)
- Wrap commit message body at 72 characters
On macOS, close a fd twice with posix_spawn_file_actions_addclose would cause posix_spawn return error 9. And the pipe doesn't need to close twice. Signed-off-by: Yibai Zhang <[email protected]>
changed |
|
Is this failing anywhere in OBS itself, or is this only failing with third-party code that calls this? Can third-party code work around this failure? |
Any code using os_process_pipe_create and os_process_pipe_create2 with flag "r" on macOS would fail, but theses scene seems only happens on Windows specificed code on OBS itself. Third party plugin could implement their own process pipe to workaround. |
|
The duplicate I'm a little bit confused by the existing code (I more or less just copied a working PR by @eric back in the day):
So for a "read pipe":
Now the code checks if the write end of the pipe is not connected to Wouldn't this mean that the child process ends up with a closed descriptor in the write end of the pipe? @kkartaltepe maybe you can run through this logic and tell me if I might have missed something? If the parent process sets So if I'm not mistaken, the first file descriptor should be closed for the child process and the second set to Thus the conditions should be removed and there should be 2 unconditional sets of |
I'm not super familiar with posix_spawn's exact semantics, but in this case it looks like it's documented that fileactions will occur immediately before cloexe closes.
This sounds more correct to me, since mainfds is created by pipe it should never be stdio/stdout. Maybe this logic was left over from the fork implementation. |
Right, by that token I'd suggest the following changes to make this a more "wholistic" PR: diff --git a/libobs/util/pipe-posix.c b/libobs/util/pipe-posix.c
index 1f5e10725..4145ff5b7 100644
--- a/libobs/util/pipe-posix.c
+++ b/libobs/util/pipe-posix.c
@@ -73,15 +73,10 @@ os_process_pipe_t *os_process_pipe_create_internal(const char *bin, char **argv,
if (process_pipe.read_pipe) {
posix_spawn_file_actions_addclose(&file_actions, mainfds[0]);
- if (mainfds[1] != STDOUT_FILENO) {
- posix_spawn_file_actions_adddup2(&file_actions, mainfds[1], STDOUT_FILENO);
- posix_spawn_file_actions_addclose(&file_actions, mainfds[0]);
- }
+ posix_spawn_file_actions_adddup2(&file_actions, mainfds[1], STDOUT_FILENO);
} else {
- if (mainfds[0] != STDIN_FILENO) {
- posix_spawn_file_actions_adddup2(&file_actions, mainfds[0], STDIN_FILENO);
- posix_spawn_file_actions_addclose(&file_actions, mainfds[1]);
- }
+ posix_spawn_file_actions_adddup2(&file_actions, mainfds[0], STDIN_FILENO);
+ posix_spawn_file_actions_addclose(&file_actions, mainfds[1]);
}
posix_spawn_file_actions_addclose(&file_actions, errfds[0]);
@@ -101,12 +96,12 @@ os_process_pipe_t *os_process_pipe_create_internal(const char *bin, char **argv,
return NULL;
}
- close(errfds[1]);
process_pipe.err_file = fdopen(errfds[0], "r");
+ close(errfds[1]);
if (process_pipe.read_pipe) {
- close(mainfds[1]);
process_pipe.file = fdopen(mainfds[0], "r");
+ close(mainfds[1]);
} else {
close(mainfds[0]);
process_pipe.file = fdopen(mainfds[1], "w");IMO that makes the code more semantically consistent, because the pair of file descriptors are always managed in their actual order in the pipe and the parent-child pairs also complement each other, so for a "read pipe" you get: So if the child does I'm not aware that the actual order of the calls to |
Description
On macOS, close a fd twice with posix_spawn_file_actions_addclose would cause posix_spawn return error 9. And the pipe doesn't need to close twice.
Motivation and Context
Fixes #12141
How Has This Been Tested?
On mac mini M4 with macOS 15.4, I extracted the related code (os_process_pipe_create_internal) and tested with a simple main function with call to os_process_pipe_create_internal and fread to pipe->readfile
Types of changes
Checklist: