Repository navigation
Description
What version of Effect is running?
@effect/sql-pg 4.0.6. Also main at 53823f2.
What steps can reproduce the bug?
Sometimes the network stops delivering traffic, but neither side closes the socket. Examples are NAT expiry, a dead route, or a frozen server. If this happens to a connection with a listen subscription, closing the listen scope does not return.
This script uses a custom stream that stops reading replies after LISTEN. Set PG_URL and run it with node.
import { PgConnection } from "@effect/sql-pg"
import { Effect, Exit, Fiber, Redacted, Scope } from "effect"
import * as Net from "node:net"
const url = new URL(process.env.PG_URL!)
const sockets: Array<Net.Socket> = []
const program = Effect.gen(function*() {
const connection = yield* PgConnection.make({
url: Redacted.make(url.toString()),
stream: () => {
const socket = Net.connect({ host: url.hostname, port: Number(url.port) })
sockets.push(socket)
return socket
}
})
const scope = yield* Scope.make()
yield* Scope.provide(connection.listen("ch"), scope)
// Stop reading replies, like a network that silently drops traffic.
for (const socket of sockets) socket.pause()
const closing = yield* Effect.forkDetach(Scope.close(scope, Exit.void))
const closed = yield* Fiber.await(closing).pipe(Effect.timeoutOption("10 seconds"))
console.log(closed._tag === "Some" ? "listen scope closed" : "listen scope still open after 10 seconds")
for (const socket of sockets) socket.destroy()
})
Effect.runPromise(Effect.scoped(program))PgClient.listen has the same problem. I also tested it with a TCP proxy that stops forwarding in both directions.
What is the expected behavior?
Scope.close returns after a short time. The session cannot answer, so it is closed and not returned to the pool.
What do you see instead?
listen scope still open after 10 seconds
With PgClient.listen and the proxy, Scope.close of the listen scope was still waiting after 20 seconds. Closing the PgClient layer was also still waiting after 20 seconds. Effect.timeout and Fiber.interrupt on the fiber that closes the scope do not help.
In production the wait lasts until the OS gives up on the socket. On Linux with the default tcp_retries2 this is about 15 minutes. I did not measure this. The number comes from the kernel defaults.
Additional information
When the last listener of a channel closes, the scope finalizer in listenChannel (packages/sql/pg/src/PgConnection.ts) runs UNLISTEN. Finalizers cannot be interrupted, and this query has no timeout. So it waits for a reply that never comes.
drainAborted already handles a stuck query. It closes the session after 5 seconds. But it only runs when a query is interrupted, and a finalizer is never interrupted. The keepAlive option does not help either. TCP keepalive probes do not run while sent data waits for an acknowledgement.
A possible fix is to give UNLISTEN a time limit. If PostgreSQL does not answer in time, close the session with conn.fatal, like drainAborted does. I have a fix with tests and can open a PR.
Activity