Skip to content

libobs/util: Fix posix_spawn failed when read pipe needed on macOS - #12147

Open
summershrimp wants to merge 1 commit into
obsproject:masterfrom
summershrimp:patch-1
Open

summershrimp wants to merge 1 commit into
obsproject:masterfrom
summershrimp:patch-1

Conversation

@summershrimp

@summershrimp summershrimp commented May 12, 2025 •

Copy link
Copy Markdown

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

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@summershrimp summershrimp changed the title libbos/util: fix posix_spawn failed when read pipe needed libbos/util: fix posix_spawn failed when read pipe needed on macOS May 12, 2025
@WizardCM WizardCM added the kind/bug Categorizes issue or PR as related to a bug. label May 12, 2025

@kkartaltepe kkartaltepe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

code is fine, the commit message should probably be something like avoiding double close.

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nits:

  • libbos -> libobs
  • Capitalize the first letter of the first word after the commit prefix (Sentence case)
  • Wrap commit message body at 72 characters

@RytoEX
RytoEX requested a review from PatTheMav May 12, 2025 19:59
@summershrimp summershrimp changed the title libbos/util: fix posix_spawn failed when read pipe needed on macOS libobs/util: fix posix_spawn failed when read pipe needed on macOS May 13, 2025
@summershrimp
summershrimp requested a review from RytoEX May 13, 2025 03:52
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]>
@summershrimp

Copy link
Copy Markdown
Author

Nits:

  • libbos -> libobs
  • Capitalize the first letter of the first word after the commit prefix (Sentence case)
  • Wrap commit message body at 72 characters

changed

@summershrimp summershrimp changed the title libobs/util: fix posix_spawn failed when read pipe needed on macOS libobs/util: Fix posix_spawn failed when read pipe needed on macOS May 13, 2025
@RytoEX RytoEX self-assigned this May 13, 2025
@RytoEX

RytoEX commented May 16, 2025

Copy link
Copy Markdown
Member

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?

@summershrimp

summershrimp commented May 17, 2025 •

Copy link
Copy Markdown
Author

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.

@PatTheMav

Copy link
Copy Markdown
Member

The duplicate posix_spawn_file_actions_addclose will indeed lead to an issue as the file descriptor will already be closed by the time the second file action is invoked by posix_spawn, however....

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):

FD_CLOEXEC is set on all file descriptors, so those would be closed before execve is called anyway if a file action hasn't closed them explicitly before.

So for a "read pipe":

  • The first file descriptor in the pipe becomes the read end for the parent process
  • The second file descriptor in the pipe becomes the write end for the child process
  • To achieve this, the first file descriptor should become inert for the child process and standard output connected to the second descriptor in the pipe

Now the code checks if the write end of the pipe is not connected to STDOUT to explicitly duplicate it into the write end of the pipe. However if it were set to STDOUT by the parent process, the condition would be false and the posix_spawn_file_actions_adddup2 call would not take place and instead the file descriptor would be closed as the result of FD_CLOEXEC.

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 STDOUT as the write end of the pipe but wants to read from it and it's closed in the child process, the parent process would only read from its own STDOUT via the pipe..

So if I'm not mistaken, the first file descriptor should be closed for the child process and the second set to STDOUT for reading, while the first one should be set to STDIN for the child and the second one closed for writing. That would also map to the opening and closing of the descriptors in the parent process further down.

Thus the conditions should be removed and there should be 2 unconditional sets of posix_spawn_file_actions_adddup2/posix_spawn_file_actions_addclose for reading and writing. Again, I might be missing something here?

@kkartaltepe

Copy link
Copy Markdown
Collaborator

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?

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.

Thus the conditions should be removed and there should be 2 unconditional sets of posix_spawn_file_actions_adddup2/posix_spawn_file_actions_addclose for reading and writing. Again, I might be missing something here?

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.

@PatTheMav

Copy link
Copy Markdown
Member

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:

parent[0]: open    <-  child[0]: closed
                    |
parent[1]: closed   -> child[1]: open

So if the child does close, open, then the parent has to do open, close (reverse order).

I'm not aware that the actual order of the calls to fdopen and close have any relevance here, so the change should be functionally inert (apart from the removal of the duplicate closing in the child process).

This branch has not been deployed

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

Labels

kind/bug Categorizes issue or PR as related to a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os_process_pipe_create with flag "r" fails on macOS

5 participants