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

Close the client connection on shutdown #112

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ const defaultPingInterval = 5 * time.Second
// API. Plugins can register with an extension manager, which handles the
// communication with the osquery process.
type ExtensionManagerServer struct {
name string
version string
sockPath string
serverClient ExtensionManager
registry map[string](map[string]OsqueryPlugin)
server thrift.TServer
transport thrift.TServerTransport
timeout time.Duration
pingInterval time.Duration // How often to ping osquery server
mutex sync.Mutex
uuid osquery.ExtensionRouteUUID
started bool // Used to ensure tests wait until the server is actually started
name string
version string
sockPath string
serverClient ExtensionManager
serverClientShouldShutdown bool // Whether to shutdown the client during server shutdown
registry map[string](map[string]OsqueryPlugin)
server thrift.TServer
transport thrift.TServerTransport
timeout time.Duration
pingInterval time.Duration // How often to ping osquery server
mutex sync.Mutex
uuid osquery.ExtensionRouteUUID
started bool // Used to ensure tests wait until the server is actually started
}

// validRegistryNames contains the allowable RegistryName() values. If a plugin
Expand Down Expand Up @@ -157,6 +158,7 @@ func NewExtensionManagerServer(name string, sockPath string, opts ...ServerOptio
return nil, err
}
manager.serverClient = serverClient
manager.serverClientShouldShutdown = true
}

return manager, nil
Expand Down Expand Up @@ -253,6 +255,11 @@ func (s *ExtensionManagerServer) Run() error {
for {
time.Sleep(s.pingInterval)

// can't ping if s.Shutdown has already happened
if s.serverClient == nil {
break
}

status, err := s.serverClient.Ping()
if err != nil {
errc <- errors.Wrap(err, "extension ping failed")
Expand Down Expand Up @@ -326,6 +333,12 @@ func (s *ExtensionManagerServer) Shutdown(ctx context.Context) (err error) {
}()
}

// Shutdown the client, if appropriate
if s.serverClientShouldShutdown && s.serverClient != nil {
s.serverClient.Close()
s.serverClient = nil
}

return
}

Expand Down