-
Notifications
You must be signed in to change notification settings - Fork 80
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
Make ExtensionManagerServer.Shutdown
idempotent
#117
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,12 +255,16 @@ func (s *ExtensionManagerServer) Run() error { | |
for { | ||
time.Sleep(s.pingInterval) | ||
|
||
s.mutex.Lock() | ||
serverClient := s.serverClient | ||
s.mutex.Unlock() | ||
|
||
// can't ping if s.Shutdown has already happened | ||
if s.serverClient == nil { | ||
if serverClient == nil { | ||
break | ||
} | ||
|
||
status, err := s.serverClient.Ping() | ||
status, err := serverClient.Ping() | ||
if err != nil { | ||
errc <- errors.Wrap(err, "extension ping failed") | ||
break | ||
|
@@ -315,12 +319,16 @@ func (s *ExtensionManagerServer) Call(ctx context.Context, registry string, item | |
func (s *ExtensionManagerServer) Shutdown(ctx context.Context) (err error) { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
stat, err := s.serverClient.DeregisterExtension(s.uuid) | ||
err = errors.Wrap(err, "deregistering extension") | ||
if err == nil && stat.Code != 0 { | ||
err = errors.Errorf("status %d deregistering extension: %s", stat.Code, stat.Message) | ||
|
||
if s.serverClient != nil { | ||
var stat *osquery.ExtensionStatus | ||
stat, err = s.serverClient.DeregisterExtension(s.uuid) | ||
err = errors.Wrap(err, "deregistering extension") | ||
if err == nil && stat.Code != 0 { | ||
err = errors.Errorf("status %d deregistering extension: %s", stat.Code, stat.Message) | ||
} | ||
} | ||
s.serverClient.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need to close ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is it closed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh. This line looks like I should have removed it in #112 |
||
|
||
if s.server != nil { | ||
server := s.server | ||
s.server = nil | ||
|
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 totally sure I understand why this helps. I think it's so that this routine can happen at the same time some other thread might have called shutdown (and set
s.serverClient
to nil), but I'm not totally sure I see it.Is this about a narrow race where shutdown happens between
if serverClient == nil
ands.serverClient.Ping()
?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.
Yes, it's narrow.
Here's the race condition if we don't add the locks (it's between the read
serverClient := s.serverClient
and the writes.serverClient = nil
inShutdown
):