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 1 commit
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
Next Next commit
feat: ruleguard to prevent pubsub.Publish in tx
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
  • Loading branch information
dannykopping committed May 16, 2025
commit 20f76bab0087a7ccb44d86efb57b89902e093ad5
32 changes: 32 additions & 0 deletions scripts/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,38 @@ 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 deadlocks or other unexpected behavior.
//
//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(`
$db.InTx(func($tx $dbType) $retType {
$*_
$ps.Publish($*args)
$*_
}, $*txopts)
`).
Where(m["ps"].Type.Implements("pubsub.Pubsub") ||
m["ps"].Text.Matches(`\w+\.pubsub`) ||
m["ps"].Text.Matches(`pubsub\.\w+`)).
Report("Avoid calling Publish inside database transactions as this may lead to connection deadlocks. Move the Publish call outside the transaction.")

// Also catch publish calls on nested fields like c.pubsub.Publish()
m.Match(`
$db.InTx(func($tx $dbType) $retType {
$*_
$ps.$field.Publish($*args)
$*_
}, $*txopts)
`).
Where(m["field"].Text == "pubsub").
Report("Avoid calling 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