[pglite-socket] Fix: Message buffering, connection handling, and concurrent connection support #775
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.
Description
Root Causes
1. Message Fragmentation
PostgreSQL wire protocol messages can span multiple TCP packets, but the implementation treated each packet as a complete message. Large queries (>64KB) would be split, causing PGlite to crash on incomplete messages.
2. Unhandled Disconnections
When clients disconnected without proper protocol termination (common with connection pooling), the exclusive lock promise rejection wasn't caught, crashing the entire server process.
3. Connection-Level Locking
The original implementation held an exclusive PGlite lock for the entire connection lifetime, preventing multiple tools from connecting simultaneously.
Solutions
Message Buffering & Reassembly
[length: 4 bytes][protocol version][parameters]
[type: 1 byte][length: 4 bytes][payload]
Robust Error Handling
Multiple Concurrent Connections
Architecture Changes
Before: One connection at a time, connection-level locking
After: Multiple connections, query-level queuing
Testing
Configuration Options
New optional configuration parameters:
Example Usage
Breaking Changes
None - existing configurations continue to work with improved behavior.
Technical Details
Message Buffer Implementation
The handler now maintains a message buffer that accumulates incoming TCP packets until a complete PostgreSQL message is available. This ensures PGlite only receives valid, complete protocol messages.
Query Queue Manager
A new
QueryQueueManager
class serializes access to PGlite at the query level rather than connection level, allowing multiple connections to coexist while maintaining PGlite's single-threaded execution model.Connection Lifecycle
Each connection is managed by a
PGLiteSocketHandler
that: