Skip to content

Conversation

@billyean
Copy link

@billyean billyean commented Nov 6, 2025

Received the following diagnostics while designing an application with postgres-nio 1.27.0...1.29.0:

00:56:35.221 [NotificationsListener.swift:517] - Notifications: Failed to LISTEN {error: "PSQLError(code: server, serverInfo: [sqlState: 57P01, file: be-secure.c, line: 209, message: terminating connection due to unexpected postmaster exit, routine: secure_read, localizedSeverity: FATAL, severity: FATAL])", topic: hawq_39}
00:56:35.222 [PostgresChannelHandler.swift:104] - Channel error caught. {psql_error: "I/O on closed channel", psql_connection_id: "1"}
00:56:35.222 [NotificationsListener.swift:286] - Notifications: Restarting subscription {topic: hawq_205}
00:56:35.222 [NotificationsListener.swift:148] - Notifications: Starting listening {psql.conn.id: "0", psql.conn.isClosed: true, topic: hawq_157}

SWIFT TASK CONTINUATION MISUSE: listen(_:) leaked its continuation without resuming it. This may cause tasks waiting on it to remain suspended forever.

And then the application deadlocks because listen() doesn't ever return.

Reproduction:

  1. The code looks like that:

             try await self.postgresClient.withConnection { connection in
                 148: log.debug("Notifications: Starting listening", metadata: [       
                     "topic": "\(topic)",                                         
                     "psql.conn.id": "\(connection.id)",                          
                     "psql.conn.isClosed": "\(connection.isClosed)"               
                 ])                                                               
                 sequence = try await connection.listen(topic.stringValue)        
                 154: log.debug("Notifications: Now listening", metadata: [            
                     "topic": "\(topic)",                                          
                     "psql.conn.id": "\(connection.id)",                          
                     "psql.conn.isClosed": "\(connection.isClosed)"               
                 ])
    
  2. brew services restart postgresql

  3. The error log similar to the log above gets generated, and the system goes into a deadlock.

Notice the log from line 148 is present, the connection appears to be CLOSED, and then the line 154 is unreachable. This might mean that listen() state machine is not correct on closed channels and result in deadlock.

@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

❌ Patch coverage is 62.50000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.84%. Comparing base (4795a0b) to head (617dd6d).

Files with missing lines Patch % Lines
...es/PostgresNIO/Connection/PostgresConnection.swift 50.00% 3 Missing ⚠️
Sources/PostgresNIO/New/NotificationListener.swift 75.00% 2 Missing ⚠️
.../Connection State Machine/ListenStateMachine.swift 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #600      +/-   ##
==========================================
- Coverage   62.51%   60.84%   -1.68%     
==========================================
  Files         132      132              
  Lines       10424     9540     -884     
==========================================
- Hits         6517     5805     -712     
+ Misses       3907     3735     -172     
Files with missing lines Coverage Δ
...urces/PostgresNIO/New/PostgresChannelHandler.swift 83.71% <100.00%> (-0.78%) ⬇️
.../Connection State Machine/ListenStateMachine.swift 65.67% <0.00%> (-3.37%) ⬇️
Sources/PostgresNIO/New/NotificationListener.swift 68.23% <75.00%> (-4.89%) ⬇️
...es/PostgresNIO/Connection/PostgresConnection.swift 40.05% <50.00%> (-0.54%) ⬇️

... and 78 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant