Move Twilio/Teams sends out of DB transactions (sensor + vitals paths)#396
Merged
Conversation
External sends (Twilio SMS / MS Teams) ran inside open SERIALIZABLE
transactions, holding a pooled pg client checked out during the (often
slow) network call. When a send was slow the transaction sat idle past
idle_in_transaction_session_timeout (60s); Postgres killed the
connection and the checked-out client emitted an unhandled 'error' ->
uncaughtException -> process exit. This removes the root cause: sends
now run AFTER commit, so no pg client is held during them.
Pattern: handlers collect deferred-send descriptors ({ describe, run })
during the transaction and return them; the caller commits, then awaits
helpers.flushDeferredSends (Promise.allSettled with per-send try/catch,
never throws). Twilio event rows stay in-transaction (their inputs don't
depend on the send); Teams sends move together with their
createTeamsEvent(messageId) write since that write needs the returned id.
The flush is awaited before the HTTP response so send-before-ack timing
is unchanged. No external-call timeouts added (a slow post-commit send
is harmless), preserving delivery and the Teams messageId chain.
Scope: sensorEvents.js (processSensorEvent/handleNewSession/
handleExistingSession) and vitals.js (checkDeviceDisconnectionVitals/
processHeartbeat). eventHandlers.js responder-reply dispatch is a
separate follow-up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
External sends (Twilio SMS / MS Teams) ran inside open SERIALIZABLE transactions, holding a pooled pg client checked out during the (often slow) network call. When a send was slow the transaction sat idle past idle_in_transaction_session_timeout (60s); Postgres killed the connection and the checked-out client emitted an unhandled 'error' -> uncaughtException -> process exit. This removes the root cause: sends now run AFTER commit, so no pg client is held during them.
Pattern: handlers collect deferred-send descriptors ({ describe, run }) during the transaction and return them; the caller commits, then awaits helpers.flushDeferredSends (Promise.allSettled with per-send try/catch, never throws). Twilio event rows stay in-transaction (their inputs don't depend on the send); Teams sends move together with their createTeamsEvent(messageId) write since that write needs the returned id. The flush is awaited before the HTTP response so send-before-ack timing is unchanged. No external-call timeouts added (a slow post-commit send is harmless), preserving delivery and the Teams messageId chain.
Scope: sensorEvents.js (processSensorEvent/handleNewSession/ handleExistingSession) and vitals.js (checkDeviceDisconnectionVitals/ processHeartbeat). eventHandlers.js responder-reply dispatch is a separate follow-up.