@@ -7,6 +7,7 @@ import { tableExists } from "../state/openclaw-state-db-schema-helpers.js";
77import {
88 closeOpenClawStateDatabaseForTest ,
99 openOpenClawStateDatabase ,
10+ registerOpenClawStateDatabaseLifecycleListener ,
1011} from "../state/openclaw-state-db.js" ;
1112import { listAuditEvents , recordAuditEvent } from "./audit-event-store.js" ;
1213import type { AuditEventInput } from "./audit-event-types.js" ;
@@ -26,6 +27,32 @@ import {
2627} from "./execution-identity-context.js" ;
2728import type { TrustedMessageAuditEvent } from "./message-audit-events.js" ;
2829
30+ function observeNonblockingSqliteTransactions (
31+ database : DatabaseSync ,
32+ observed : number [ ] ,
33+ ) : ( ) => void {
34+ const originalExecDescriptor = Object . getOwnPropertyDescriptor ( database , "exec" ) ;
35+ const originalExec = database . exec . bind ( database ) ;
36+ database . exec = ( sql : string ) => {
37+ if ( sql === "BEGIN IMMEDIATE" ) {
38+ const busyTimeout = readSqliteBusyTimeout ( database ) ;
39+ observed . push ( busyTimeout ) ;
40+ if ( busyTimeout !== 0 ) {
41+ throw new Error ( `audit writer attempted a blocking SQLite transaction (${ busyTimeout } ms)` ) ;
42+ }
43+ }
44+ return originalExec ( sql ) ;
45+ } ;
46+ return ( ) => {
47+ if ( originalExecDescriptor ) {
48+ Object . defineProperty ( database , "exec" , originalExecDescriptor ) ;
49+ return ;
50+ }
51+ const ownDatabaseMethod : { exec ?: DatabaseSync [ "exec" ] } = database ;
52+ delete ownDatabaseMethod . exec ;
53+ } ;
54+ }
55+
2956function defineObjectPrototypeProperties ( descriptors : PropertyDescriptorMap ) : void {
3057 // oxlint-disable-next-line no-extend-native -- Exercise hostile prototype pollution across the real clone boundary.
3158 Object . defineProperties ( Object . prototype , descriptors ) ;
@@ -262,20 +289,40 @@ describe("audit event writer", () => {
262289 const contender = new DatabaseSync ( path ) ;
263290 contender . exec ( "PRAGMA busy_timeout = 0; BEGIN IMMEDIATE" ) ;
264291 const errors : string [ ] = [ ] ;
265- const probeStartedAt = performance . now ( ) ;
292+ const observedBusyTimeouts : number [ ] = [ ] ;
293+ let openedBusyTimeout : number | undefined ;
294+ let restoreExec : ( ( ) => void ) | undefined ;
295+ const clearDatabaseListener = registerOpenClawStateDatabaseLifecycleListener ( ( event ) => {
296+ if ( event . kind !== "opened" || event . database . path !== path ) {
297+ return ;
298+ }
299+ openedBusyTimeout = readSqliteBusyTimeout ( event . database . db ) ;
300+ restoreExec = observeNonblockingSqliteTransactions ( event . database . db , observedBusyTimeouts ) ;
301+ } ) ;
266302 const writer = createAuditEventWriter ( { stateDir, onError : ( error ) => errors . push ( error ) } ) ;
267303
268304 try {
269- const eventLoopDelay = await new Promise < number > ( ( resolve ) => {
270- setTimeout ( ( ) => resolve ( performance . now ( ) - probeStartedAt ) , 25 ) ;
271- } ) ;
272- expect ( eventLoopDelay ) . toBeLessThan ( 250 ) ;
273305 await writer . ready ;
306+ await new Promise < void > ( ( resolve ) => {
307+ setImmediate ( resolve ) ;
308+ } ) ;
309+ expect ( contender . isTransaction ) . toBe ( true ) ;
310+ expect ( openedBusyTimeout ) . toBe ( 0 ) ;
311+ expect ( observedBusyTimeouts ) . not . toHaveLength ( 0 ) ;
312+ expect ( observedBusyTimeouts . every ( ( busyTimeout ) => busyTimeout === 0 ) ) . toBe ( true ) ;
274313 expect ( writer . record ( { ...input ( ) , sourceId : "cold-owner" , runId : "cold-owner" } ) ) . toBe ( true ) ;
275314 } finally {
276- contender . exec ( "ROLLBACK" ) ;
277- contender . close ( ) ;
278- await writer . stop ( ) ;
315+ try {
316+ contender . exec ( "ROLLBACK" ) ;
317+ contender . close ( ) ;
318+ } finally {
319+ try {
320+ await writer . stop ( ) ;
321+ } finally {
322+ restoreExec ?.( ) ;
323+ clearDatabaseListener ( ) ;
324+ }
325+ }
279326 }
280327
281328 expect ( errors ) . toEqual ( [ ] ) ;
@@ -342,6 +389,8 @@ describe("audit event writer", () => {
342389 db . exec ( "DELETE FROM audit_identity_keys;" ) ;
343390 const contender = new DatabaseSync ( path ) ;
344391 contender . exec ( "PRAGMA busy_timeout = 0; BEGIN IMMEDIATE" ) ;
392+ const observedBusyTimeouts : number [ ] = [ ] ;
393+ const restoreExec = observeNonblockingSqliteTransactions ( db , observedBusyTimeouts ) ;
345394 const clearSink = configureExecutionIdentityAdmissionSink ( writer . recordExecutionIdentity ) ;
346395 const admittedAt = Date . now ( ) ;
347396
@@ -380,11 +429,12 @@ describe("audit event writer", () => {
380429 accepted : true ,
381430 } ) ;
382431 expect ( performance . now ( ) - startedAt ) . toBeLessThan ( 250 ) ;
383- const eventLoopProbeStartedAt = performance . now ( ) ;
384- const eventLoopDelay = await new Promise < number > ( ( resolve ) => {
385- setTimeout ( ( ) => resolve ( performance . now ( ) - eventLoopProbeStartedAt ) , 25 ) ;
432+ await new Promise < void > ( ( resolve ) => {
433+ setImmediate ( resolve ) ;
386434 } ) ;
387- expect ( eventLoopDelay ) . toBeLessThan ( 250 ) ;
435+ expect ( contender . isTransaction ) . toBe ( true ) ;
436+ expect ( observedBusyTimeouts ) . not . toHaveLength ( 0 ) ;
437+ expect ( observedBusyTimeouts . every ( ( busyTimeout ) => busyTimeout === 0 ) ) . toBe ( true ) ;
388438 expect ( readSqliteBusyTimeout ( db ) ) . toBe ( 5_000 ) ;
389439 expect (
390440 writer . recordExecutionIdentity ( {
@@ -411,7 +461,11 @@ describe("audit event writer", () => {
411461 contender . close ( ) ;
412462 } finally {
413463 clearSink ( ) ;
414- await writer . stop ( ) ;
464+ try {
465+ await writer . stop ( ) ;
466+ } finally {
467+ restoreExec ( ) ;
468+ }
415469 }
416470 }
417471
0 commit comments