-
Notifications
You must be signed in to change notification settings - Fork 469
fix(printer): properly close output files to prevent data loss #4998
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
base: main
Are you sure you want to change the base?
Changes from all 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |||||
| "io" | ||||||
| "net/http" | ||||||
| "net/url" | ||||||
| "os" | ||||||
| "path/filepath" | ||||||
| "strconv" | ||||||
| "strings" | ||||||
|
|
@@ -349,6 +350,10 @@ func (p tableEventPrinter) Epilogue(stats metrics.Stats) { | |||||
| } | ||||||
|
|
||||||
| func (p tableEventPrinter) Close() { | ||||||
| // Sync flushes buffered data, ensuring events aren't lost on process exit | ||||||
| if f, ok := p.out.(*os.File); ok && f != nil { | ||||||
| _ = f.Sync() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| type templateEventPrinter struct { | ||||||
|
|
@@ -389,6 +394,10 @@ func (p templateEventPrinter) Print(event trace.Event) { | |||||
| func (p templateEventPrinter) Epilogue(stats metrics.Stats) {} | ||||||
|
|
||||||
| func (p templateEventPrinter) Close() { | ||||||
| // Sync flushes buffered data, ensuring events aren't lost on process exit | ||||||
| if f, ok := p.out.(*os.File); ok && f != nil { | ||||||
|
||||||
| if f, ok := p.out.(*os.File); ok && f != nil { | |
| if f, ok := p.out.(*os.File); ok { |
Copilot
AI
Oct 28, 2025
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.
The nil check f != nil is redundant after a successful type assertion. If the type assertion succeeds, f cannot be nil. Consider simplifying to if f, ok := p.out.(*os.File); ok {.
| if f, ok := p.out.(*os.File); ok && f != nil { | |
| if f, ok := p.out.(*os.File); ok { |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -135,3 +135,121 @@ func TestTemplateEventPrinterSprigFunctions(t *testing.T) { | |||||
| // Should contain timestamp | ||||||
| assert.Contains(t, output, `1234567890`, "timestamp should be included") | ||||||
| } | ||||||
|
|
||||||
| // TestPrinterCloseFlusheData tests that Close() calls Sync() to flush buffered data to disk | ||||||
|
||||||
| // TestPrinterCloseFlusheData tests that Close() calls Sync() to flush buffered data to disk | |
| // TestPrinterCloseFlushesData tests that Close() calls Sync() to flush buffered data to disk |
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.
The nil check
f != nilis redundant after a successful type assertion. If the type assertion succeeds, f cannot be nil. Consider simplifying toif f, ok := p.out.(*os.File); ok {.