-
Notifications
You must be signed in to change notification settings - Fork 65
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
fix: Prevent calling the main process exit without WithOnClose() #108
base: main
Are you sure you want to change the base?
Conversation
a561814
to
0e9b428
Compare
Signed-off-by: attlee-wang <wangdong102425@gmail.com>
0e9b428
to
49114f6
Compare
@attlee-wang Not directly related to this PR, but it might be useful for you: ever since we have merged PR #91, it became possible to restart a stub by using a
This behavior has been discussed and criticized earlier a few times, and it is fair to say that not without a reason. I have a few question/comments related to this.
|
@@ -543,7 +544,7 @@ func (stub *stub) connClosed() { | |||
return | |||
} | |||
|
|||
os.Exit(0) | |||
runtime.Goexit() //The exit code can be determined when the coroutine exits |
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 not sure why a runtime.Goexit()
would be better here than not doing anything (other than maybe recording the fact that we lost connection) and simply returning instead? If the stub has been started by stub.Start()
or stub.Run()
, it should be sitting in ttrpc.Server.Serve()
which then should (soon) return once we return from here. That in turn should cause stub.Run()
to return, if that is what the caller used to start the stub, or otherwise stub.Wait()
to return if that is what the caller uses to wait for the stub to stop.
I think what we could do here is only call os.Exit()
if this is a pre-connected plugin. So,
- either record in
connect()
the fact that we've taken asocketpair
from the env., and - use that to decide to
os.Exit()
here, or check here for the presence of the same env. var and simply use that fact to exit
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.
Or I think something like this could work, too:
diff --git a/pkg/stub/stub.go b/pkg/stub/stub.go
index 88fd2e1..b68344f 100644
--- a/pkg/stub/stub.go
+++ b/pkg/stub/stub.go
@@ -502,6 +502,15 @@ func (stub *stub) connect() error {
return fmt.Errorf("invalid socket (%d) in environment: %w", fd, err)
}
+ userOnClose := stub.onClose
+ stub.onClose = func() {
+ if userOnClose != nil {
+ userOnClose()
+ }
+ log.Infof(noCtx, "Connection from environment lost, exiting...")
+ os.Exit(0)
+ }
+
return nil
}
ping @attlee-wang |
@klihub It's not that I don't use WithOnClose(). I looked at the stub.go package and exist stub.Stop(). I subjectively thought that stub.Stop() could be used. After all, WithOnClose() is just an optional function. |
My service is a multi-goroutine daemon service, and nri-plugin is one of the modules of my service.
In nri-plugin I did not use WithOnClose() to set the exit callback function, I would rebuild a new stub when exiting. I found that when the stup exception occurred, the daemon service main was actually exited. I felt that it was very unfriendly to exit the deamon service due to a goroutine exception.
After checking, it is found that there is a problem with the following code:
os.Exit(0) exits the process,for the nri module package, using runtime.Goexit() is more elegant. runtime.Goexit() will only exit the current goroutine, not the daemon process.