tsnet: fix closing of filch buffer (#5299)

It should be safe to initialize multiple Server instances
without any resource leaks what-so-ever.

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai 2022-08-04 13:22:56 -07:00 committed by GitHub
parent f50043f6cb
commit ddebd30917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,6 +91,7 @@ type Server struct {
shutdownCtx context.Context shutdownCtx context.Context
shutdownCancel context.CancelFunc shutdownCancel context.CancelFunc
localClient *tailscale.LocalClient localClient *tailscale.LocalClient
logbuffer *filch.Filch
logtail *logtail.Logger logtail *logtail.Logger
mu sync.Mutex mu sync.Mutex
@ -138,6 +139,7 @@ func (s *Server) Close() error {
defer wg.Done() defer wg.Done()
// Perform a best-effort final flush. // Perform a best-effort final flush.
s.logtail.Shutdown(ctx) s.logtail.Shutdown(ctx)
s.logbuffer.Close()
}() }()
if _, isMemStore := s.Store.(*mem.Store); isMemStore && s.Ephemeral { if _, isMemStore := s.Store.(*mem.Store); isMemStore && s.Ephemeral {
@ -238,7 +240,7 @@ func (s *Server) start() error {
} }
logid := lpc.PublicID.String() logid := lpc.PublicID.String()
f, err := filch.New(filepath.Join(s.rootPath, "tailscaled"), filch.Options{ReplaceStderr: false}) s.logbuffer, err = filch.New(filepath.Join(s.rootPath, "tailscaled"), filch.Options{ReplaceStderr: false})
if err != nil { if err != nil {
return fmt.Errorf("error creating filch: %w", err) return fmt.Errorf("error creating filch: %w", err)
} }
@ -246,7 +248,7 @@ func (s *Server) start() error {
Collection: lpc.Collection, Collection: lpc.Collection,
PrivateID: lpc.PrivateID, PrivateID: lpc.PrivateID,
Stderr: ioutil.Discard, // log everything to Buffer Stderr: ioutil.Discard, // log everything to Buffer
Buffer: f, Buffer: s.logbuffer,
NewZstdEncoder: func() logtail.Encoder { NewZstdEncoder: func() logtail.Encoder {
w, err := smallzstd.NewEncoder(nil) w, err := smallzstd.NewEncoder(nil)
if err != nil { if err != nil {