Skip to content

Commit

Permalink
Fix possible leak
Browse files Browse the repository at this point in the history
Not closing os.File must not, but may lead to a leak. It is better to close it manually instead of waiting for the next GC to be triggered

Signed-off-by: Atanas Alexandrov <sirakov@gmail.com>
  • Loading branch information
cupakob committed Oct 30, 2024
1 parent e7d1d54 commit cc8dff6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/sharded-test-server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func startCacheServer(ctx context.Context, logDirPath, workingDir string, synthe
return nil, "", err
}

defer func() {
err = logFile.Close()
if err != nil {
klog.ErrorS(err, "failed to close the log file")
}
}()

writer := helpers.NewHeadWriter(logFile, out)
cmd.Stdout = writer
cmd.Stdin = os.Stdin
Expand Down
7 changes: 7 additions & 0 deletions cmd/sharded-test-server/frontproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func startFrontProxy(
return err
}

defer func() {
err = logFile.Close()
if err != nil {
klog.ErrorS(err, "failed to close the log file")
}
}()

writer := helpers.NewHeadWriter(logFile, out)
cmd.Stdout = writer
cmd.Stdin = os.Stdin
Expand Down
7 changes: 7 additions & 0 deletions cmd/sharded-test-server/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ func (v *VirtualWorkspace) start(ctx context.Context) error {
return err
}

defer func() {
err = logFile.Close()
if err != nil {
klog.ErrorS(err, "failed to close the log file")
}
}()

v.writer = helpers.NewHeadWriter(logFile, out)
cmd.Stdout = v.writer
cmd.Stdin = os.Stdin
Expand Down
7 changes: 7 additions & 0 deletions cmd/test-server/kcp/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ func (s *Shard) Start(ctx context.Context, quiet bool) error {
return err
}

defer func() {
err = logFile.Close()
if err != nil {
klog.ErrorS(err, "failed to close the log file")
}
}()

s.writer = helpers.NewHeadWriter(logFile, out)
cmd.Stdout = s.writer
cmd.Stdin = os.Stdin
Expand Down

0 comments on commit cc8dff6

Please sign in to comment.