mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 23:24:21 +00:00
fix(runner): enforce ownership and safe reconnects
Gate dispatch on reconciliation, retain cancellations across disconnects, and stop replaced clients. Guard the single-server runner topology with a database lease. Restage missing generated inputs, report signal exits, and bound generated-file cleanup.
This commit is contained in:
@@ -39,3 +39,68 @@ export async function acquireAdvisoryLock(key: string, signal?: AbortSignal) {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** Hold a shared server lease, with a non-blocking upgrade to exclusive ownership. */
|
||||
export async function tryAcquireUpgradeableAdvisoryLock(key: string, exclusive: boolean) {
|
||||
const client = await locks.connect()
|
||||
const lost = new AbortController()
|
||||
const onError = (error: Error) => lost.abort(error)
|
||||
client.on('error', onError)
|
||||
|
||||
const tryExclusive = async () => {
|
||||
const result = await client.query<{ acquired: boolean }>(
|
||||
'SELECT pg_try_advisory_lock(hashtextextended($1, 0)) AS acquired',
|
||||
[key],
|
||||
)
|
||||
return result.rows[0]?.acquired === true
|
||||
}
|
||||
let acquired = false
|
||||
try {
|
||||
if (exclusive && !(await tryExclusive())) return null
|
||||
const result = await client.query<{ acquired: boolean }>(
|
||||
'SELECT pg_try_advisory_lock_shared(hashtextextended($1, 0)) AS acquired',
|
||||
[key],
|
||||
)
|
||||
lost.signal.throwIfAborted()
|
||||
acquired = result.rows[0]?.acquired === true
|
||||
if (!acquired) return null
|
||||
} finally {
|
||||
if (!acquired) {
|
||||
client.off('error', onError)
|
||||
client.release(true)
|
||||
}
|
||||
}
|
||||
|
||||
let released = false
|
||||
const assertHeld = () => {
|
||||
if (released) throw new Error('The server lease has been released')
|
||||
lost.signal.throwIfAborted()
|
||||
}
|
||||
return {
|
||||
signal: lost.signal,
|
||||
get exclusive() {
|
||||
return exclusive
|
||||
},
|
||||
async tryUpgrade() {
|
||||
assertHeld()
|
||||
if (!exclusive) exclusive = await tryExclusive()
|
||||
return exclusive
|
||||
},
|
||||
async downgrade() {
|
||||
assertHeld()
|
||||
if (!exclusive) return
|
||||
await client.query('SELECT pg_advisory_unlock(hashtextextended($1, 0))', [key])
|
||||
exclusive = false
|
||||
},
|
||||
async release() {
|
||||
if (released) return
|
||||
released = true
|
||||
try {
|
||||
if (!lost.signal.aborted) await client.query('SELECT pg_advisory_unlock_all()')
|
||||
} finally {
|
||||
client.off('error', onError)
|
||||
client.release(true)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user