-
Notifications
You must be signed in to change notification settings - Fork 43
fix: refactor coder logger to allow flush without deadlock #375
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
Conversation
34365d5
to
cf30d8b
Compare
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.
Refactor LGTM 👍
closeOnce.Do(func() { | ||
// Trigger a flush and wait for logs to be sent. | ||
ls.Flush(uid) | ||
ctx, cancel := context.WithTimeout(ctx, logSendGracePeriod) |
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.
nit: should we merge context.WithTimeout
and context.WithCancel
and use one above?
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.
It's doable, but I feel their purpose is clearer as-is. We could actually make sendCtx not inherit function ctx instead but it's also pointless because ConnectRPC20
applies ctx to connection, so messages won't go through.
@@ -44,18 +46,20 @@ func Coder(ctx context.Context, coderURL *url.URL, token string) (Func, func(), | |||
} | |||
if semver.Compare(semver.MajorMinor(bi.Version), minAgentAPIV2) < 0 { | |||
metaLogger.Warn(ctx, "Detected Coder version incompatible with AgentAPI v2, falling back to deprecated API", slog.F("coder_version", bi.Version)) | |||
sendLogs, flushLogs := sendLogsV1(ctx, client, metaLogger.Named("send_logs_v1")) | |||
return sendLogs, flushLogs, nil | |||
logger, closer = sendLogsV1(ctx, client, metaLogger.Named("send_logs_v1")) |
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.
Nice 👍 I found those previous names hard to reason about.
l.Warn(ctx, "failed to flush logs", slog.Error(err)) | ||
} | ||
} | ||
} | ||
|
||
// sendLogsV2 uses the v2 agent API to send logs. Only compatibile with coder versions >= 2.9. | ||
func sendLogsV2(ctx context.Context, dest agentsdk.LogDest, ls coderLogSender, l slog.Logger) (Func, func()) { | ||
func sendLogsV2(ctx context.Context, dest agentsdk.LogDest, ls coderLogSender, l slog.Logger) (logger Func, closer func()) { | ||
sendCtx, sendCancel := context.WithCancel(ctx) |
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.
Should sendCancel
be deferred, too?
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.
Nope, we want to keep this context alive until we've flushed or hit the timeout in the closer
function.
1470637
to
5243969
Compare
(cherry picked from commit 8170323)
Fixes #364