From 2a8a147e7de86608216a3cf7b6f228ef276bb0f0 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Wed, 22 Feb 2023 16:07:26 -0600 Subject: [PATCH] fix: don't log disconnect error when the database is shutting down (#6309) * fix: don't log disconnect error when the database is shutting down Seen in: https://github.com/coder/coder/actions/runs/4244980490/jobs/7379867681 * Generalize the query close error --- coderd/database/errors.go | 10 ++++++++++ coderd/workspaceagents.go | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/coderd/database/errors.go b/coderd/database/errors.go index 5028e6281a..9a7c04cd5f 100644 --- a/coderd/database/errors.go +++ b/coderd/database/errors.go @@ -27,3 +27,13 @@ func IsUniqueViolation(err error, uniqueConstraints ...UniqueConstraint) bool { return false } + +// IsQueryCanceledError checks if the error is due to a query being canceled. +func IsQueryCanceledError(err error) bool { + var pqErr *pq.Error + if errors.As(err, &pqErr) { + return pqErr.Code.Name() == "query_canceled" + } + + return false +} diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index 51769a66a5..3c6a4d4324 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -644,7 +644,9 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request // This is a bug with unit tests that cancel the app context and // cause this error log to be generated. We should fix the unit tests // as this is a valid log. - if !xerrors.Is(err, context.Canceled) { + // + // The pq error occurs when the server is shutting down. + if !xerrors.Is(err, context.Canceled) && !database.IsQueryCanceledError(err) { api.Logger.Error(ctx, "failed to update agent disconnect time", slog.Error(err), slog.F("workspace", build.WorkspaceID),