See the best posts from any Bluesky account
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix test process hanging by eliminating leaked timers in JetstreamConsumer

Replace waitForShutdown() polling (recursive setTimeout) with a direct
promise resolved by shutdown(), and unref() the DID-refresh and
buffer-flush intervals so they don't prevent process exit when tests
fail before calling shutdown().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+11 -8
+11 -8
app/services/jetstream_consumer.ts
··· 131 131 * Current reconnect backoff in milliseconds. Resets to 1000 on success. 132 132 */ 133 133 private reconnectBackoffMs = 1000 134 + private shutdownResolve: (() => void) | null = null 134 135 135 136 // ------------------------------------------------------------------------- 136 137 // Constructor ··· 158 159 await this.connect() 159 160 160 161 // 3. Set up 1s tracked-DID refresh 162 + // unref() so leaked timers don't prevent process exit in tests 161 163 this.didRefreshTimer = setInterval(() => { 162 164 void this.refreshTrackedDids() 163 165 }, 1000) 166 + this.didRefreshTimer.unref() 164 167 165 168 // 4. Set up 500ms / 1000-row flush interval 166 169 this.bufferFlushTimer = setInterval(() => { 167 170 void this.flushIfNeeded() 168 171 }, 500) 172 + this.bufferFlushTimer.unref() 169 173 170 174 // 5. Wait until shutdown is requested 171 175 await this.waitForShutdown() ··· 193 197 if (this.ws !== null) { 194 198 this.ws.close() 195 199 this.ws = null 200 + } 201 + 202 + if (this.shutdownResolve !== null) { 203 + this.shutdownResolve() 204 + this.shutdownResolve = null 196 205 } 197 206 } 198 207 ··· 549 558 // ------------------------------------------------------------------------- 550 559 551 560 private waitForShutdown(): Promise<void> { 561 + if (this.shutdownRequested) return Promise.resolve() 552 562 return new Promise((resolve) => { 553 - const check = () => { 554 - if (this.shutdownRequested) { 555 - resolve() 556 - } else { 557 - setTimeout(check, 100) 558 - } 559 - } 560 - check() 563 + this.shutdownResolve = resolve 561 564 }) 562 565 } 563 566 }