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

fix: refactor coder logger to allow flush without deadlock #375

Merged
merged 8 commits into from
Sep 30, 2024

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Sep 30, 2024

Fixes #364

Copy link
Member

@mtojek mtojek left a 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)
Copy link
Member

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?

Copy link
Member Author

@mafredri mafredri Sep 30, 2024

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.

log/coder.go Outdated Show resolved Hide resolved
cmd/envbuilder/main.go Show resolved Hide resolved
@@ -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"))
Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Member Author

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.

log/coder_internal_test.go Outdated Show resolved Hide resolved
@mafredri mafredri enabled auto-merge (squash) September 30, 2024 13:48
@mafredri mafredri merged commit 8170323 into main Sep 30, 2024
4 checks passed
@mafredri mafredri deleted the mafredri/logssss branch September 30, 2024 13:52
mafredri added a commit that referenced this pull request Sep 30, 2024
mafredri added a commit that referenced this pull request Sep 30, 2024
mafredri added a commit that referenced this pull request Oct 2, 2024
mafredri added a commit that referenced this pull request Oct 2, 2024
mafredri added a commit that referenced this pull request Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Defers run in cmd/envbuilder won't execute because Run performs execve(2)
3 participants