Skip to content

Specify specific cancel cause on shutdown and use it to determine error logging#179

Merged
brandur merged 1 commit intomasterfrom
brandur-shutdown-context-cause
Jan 28, 2024
Merged

Specify specific cancel cause on shutdown and use it to determine error logging#179
brandur merged 1 commit intomasterfrom
brandur-shutdown-context-cause

Conversation

@brandur
Copy link
Contributor

@brandur brandur commented Jan 27, 2024

Follow up to a discussion [1] in which an additional error log was
causing example tests to fail after uses of log were replaced with
slog and no longer suppressed.

Here, use WithCancelCause to send a specific cancellation error on
shutdown, which can be handled specially for instances where we'd only
want to log an error under unusual circumstances.

[1] #140 (comment)

@brandur brandur marked this pull request as draft January 27, 2024 20:14
@brandur brandur force-pushed the brandur-shutdown-context-cause branch 2 times, most recently from fb41aa7 to 99a2378 Compare January 27, 2024 20:29

conn, err := n.establishConn(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I wasn't quite able to figure out is why this condition wasn't detecting the cancellation previously. I would've thought that even without a special cancellation error, this still would've been enough to intercept a problem before falling through to logging.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, hard to tell without digging into the code, but maybe it's possible pgx doesn't surface this source error correctly? establishConn is a simple method that just returns the result of pgx.ConnectConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, that's probably it actually. The error being checked is now coming from a different place (context), which would explain why this version works.

@brandur brandur marked this pull request as ready for review January 27, 2024 20:32
@brandur brandur requested a review from bgentry January 27, 2024 20:32
…or logging

Follow up to a discussion [1] in which an additional error log was
causing example tests to fail after uses of `log` were replaced with
`slog` and no longer suppressed.

Here, use `WithCancelCause` to send a specific cancellation error on
shutdown, which can be handled specially for instances where we'd only
want to log an error under unusual circumstances.

[1] #140 (comment)
@brandur brandur force-pushed the brandur-shutdown-context-cause branch from 99a2378 to 1f236a6 Compare January 27, 2024 20:37
@brandur
Copy link
Contributor Author

brandur commented Jan 27, 2024

@bgentry This one's a follow up from the conversation in #140 (comment) — roughly what you had in mind? I think this solves the logging problem that was failing example tests before. If I recall correctly, the failures we were seeing before the special patch in #140 would reproduce quite reliably. I reran CI a few times here to try and make a failure happen, but it seems to be fairly stable as written.

}

stopCtx, stopCancel := context.WithTimeout(ctx, 5*time.Second)
t.Cleanup(stopCancel)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took this out because with the addition of require.ErrorIs above, it revealed that it occasionally causes an intermittent failure as it kicks in and supersedes the shutdown's cancellation. I don't think this is needed anyway because if shutdown's context cancellation doesn't succeed, I don't think we could expect this one to succeed either. Also, not having keeps the test more succinct.

Copy link
Contributor

@bgentry bgentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've been meaning to take a pass through some places like this and add specific cancellation causes where possible (ever since we started using those APIs).


conn, err := n.establishConn(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, hard to tell without digging into the code, but maybe it's possible pgx doesn't surface this source error correctly? establishConn is a simple method that just returns the result of pgx.ConnectConfig

@brandur
Copy link
Contributor Author

brandur commented Jan 28, 2024

Thanks!

I'll skip a changelog entry on this one since as written, it's not a user-facing change.

@brandur brandur merged commit 92bcd23 into master Jan 28, 2024
@brandur brandur deleted the brandur-shutdown-context-cause branch January 28, 2024 15:17
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.

2 participants