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
Prev Previous commit
Next Next commit
use 'Is' instead of 'Implements'
  • Loading branch information
Emyrk committed May 16, 2025
commit fe31f5d834c9bd940909fdccdd6ee840495ee03f
26 changes: 11 additions & 15 deletions scripts/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,22 @@ func publishInTransaction(m dsl.Matcher) {

// Match direct calls to the Publish method of a pubsub instance inside InTx
m.Match(`
$db.InTx(func($tx $dbType) $retType {
$x.InTx(func($y) error {
$*_
$ps.Publish($*args)
$_ = $ps.Publish($evt, $msg)
$*_
}, $*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 {
}, $*_)
`,
// Without catching error return
`
$x.InTx(func($y) error {
$*_
$ps.$field.Publish($*args)
$ps.Publish($evt, $msg)
$*_
}, $*txopts)
}, $*_)
`).
Where(m["field"].Text == "pubsub").
Where(m["ps"].Type.Is("pubsub.Pubsub")).
At(m["ps"]).
Report("Avoid calling Publish inside database transactions as this may lead to connection deadlocks. Move the Publish call outside the transaction.")
}

Expand Down