05nelsonm / kmp-process

Process implementation for Kotlin Multiplatform
Apache License 2.0
30 stars 1 forks source link

Potential race condition with `posix_spawn` #101

Open 05nelsonm opened 4 months ago

05nelsonm commented 4 months ago

Getting errors on iOS sometimes where stdout has no output and the program does nothing (either a zombie process, or something is inhibiting it from further execution). iOS uses posix_spawn exclusively which is why I think there is a race condition going on between the process spawning, and when descriptors are closed for the unused pipe ends in the parent process.

NOTE: It'd be "safe" to leave the unused ends of the pipes open in the parent because CLOEXEC is set.

Because posix_spawn is sort of a black box, this is incredibly difficult to test, but my hunch is that it's attributed to the almost immediate closure of those unused pipe ends which may not have been dup'd yet in the child.

05nelsonm commented 4 months ago

And like that, the error presents itself in CI (even with the above "fix") https://github.com/05nelsonm/kmp-process/actions/runs/9316330728/job/25644357709#step:9:360

This is either an iOS, or an x86_64 simulator thing...

05nelsonm commented 3 months ago

Seems to be an issue related to spawning the process from a background thread? Maybe advantageous for native to investigate using runBlocking + Dispatchers.Main???