Skip to content
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

proposal: runtime: detect calls to fork in C code (by observing PID change after cgocall return) #67174

Open
upsampled opened this issue May 4, 2024 · 0 comments
Labels
Milestone

Comments

@upsampled
Copy link

Proposal Details

Summary

I believe that especially when go code is being run as a C library, there should be optional detection and warning for actions that can cause a process to hang.

Using the fork() issue detailed in #53806 as an example, this might look like:
An optional build argument would trigger the runtime to check the process PID before it performs calls to the goroutine scheduler that might hang. If the PID changes, a fork occurred, and either a panic or a simple warning to stderr is performed.

Background

Recently I switched a codebase from github.com/mattn/go-sqlite3, to crawshaw.io/sqlite/sqlitex for accessing a file sqlite3 db. The codebase produces a couple go programs and a c-shared library that is used by a legacy app. Upon testing this change the legacy app's child processes hung. After hours of ruling out sqlite3, I finally saw #53806. I am assuming that because mattn does not use goroutines, it avoided the issue while crawshaw used goroutines to manage connection pools and hung the child processes.

Reading about the core issue, fork only copies a single thread, it is pretty easy to see why the issue occurs. Go also does a great job of making it hard to fork within Go code. The problem really is that C programs are already calling fork() and the Go code may or may not hang because of it. This really hides the root cause of the issue and I am hoping that this proposal makes it more clear.

@gopherbot gopherbot added this to the Proposal milestone May 4, 2024
@seankhliao seankhliao changed the title proposal: runtime/c-shared: fork() and other dangerous actions warning proposal: runtime/cgo: fork() and other dangerous actions warning May 4, 2024
@adonovan adonovan changed the title proposal: runtime/cgo: fork() and other dangerous actions warning proposal: runtime: detect calls to fork in C code (by observing PID change after cgocall return) May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

2 participants