Skip to content

feat: fail CI when pubsub.Publish calls are found in db transactions #17903

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

Merged
merged 8 commits into from
May 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions scripts/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ func databaseImport(m dsl.Matcher) {
Where(m.File().PkgPath.Matches("github.com/coder/coder/v2/codersdk"))
}

// publishInTransaction detects calls to Publish inside database transactions
// which can lead to connection starvation.
//
//nolint:unused,deadcode,varnamelen
func publishInTransaction(m dsl.Matcher) {
m.Import("github.com/coder/coder/v2/coderd/database/pubsub")

// Match direct calls to the Publish method of a pubsub instance inside InTx
m.Match(`
$_.InTx(func($x) error {
$*_
$_ = $ps.Publish($evt, $msg)
$*_
}, $*_)
`,
// Alternative with short variable declaration
`
$_.InTx(func($x) error {
$*_
$_ := $ps.Publish($evt, $msg)
$*_
}, $*_)
`,
// Without catching error return
`
$_.InTx(func($x) error {
$*_
$ps.Publish($evt, $msg)
$*_
}, $*_)
`).
Where(m["ps"].Type.Is("pubsub.Pubsub")).
At(m["ps"]).
Report("Avoid calling pubsub.Publish() inside database transactions as this may lead to connection deadlocks. Move the Publish() call outside the transaction.")
}

// doNotCallTFailNowInsideGoroutine enforces not calling t.FailNow or
// functions that may themselves call t.FailNow in goroutines outside
// the main test goroutine. See testing.go:834 for why.
Expand Down
Loading