MCPcopy Index your code
hub / github.com/coder/coder / publishInTransaction

Function publishInTransaction

scripts/rules.go:146–176  ·  view source on GitHub ↗

publishInTransaction detects calls to Publish inside database transactions which can lead to connection starvation. nolint:unused,deadcode,varnamelen

(m dsl.Matcher)

Source from the content-addressed store, hash-verified

144//
145//nolint:unused,deadcode,varnamelen
146func publishInTransaction(m dsl.Matcher) {
147 m.Import("github.com/coder/coder/v2/coderd/database/pubsub")
148
149 // Match direct calls to the Publish method of a pubsub instance inside InTx
150 m.Match(`
151 $_.InTx(func($x) error {
152 $*_
153 $_ = $ps.Publish($evt, $msg)
154 $*_
155 }, $*_)
156 `,
157 // Alternative with short variable declaration
158 `
159 $_.InTx(func($x) error {
160 $*_
161 $_ := $ps.Publish($evt, $msg)
162 $*_
163 }, $*_)
164 `,
165 // Without catching error return
166 `
167 $_.InTx(func($x) error {
168 $*_
169 $ps.Publish($evt, $msg)
170 $*_
171 }, $*_)
172 `).
173 Where(m["ps"].Type.Is("pubsub.Pubsub")).
174 At(m["ps"]).
175 Report("Avoid calling pubsub.Publish() inside database transactions as this may lead to connection deadlocks. Move the Publish() call outside the transaction.")
176}
177
178// doNotCallTFailNowInsideGoroutine enforces not calling t.FailNow or
179// functions that may themselves call t.FailNow in goroutines outside

Callers

nothing calls this directly

Calls 3

MatchMethod · 0.80
ReportMethod · 0.65
IsMethod · 0.45

Tested by

no test coverage detected