Back to Handle Lifecycle

Interprocess Communication (IPC) · Handle Lifecycle · Practice Question

Detect When a Remote Connection Has Closed

Sign in to track your progress

Question

If a q process has opened a connection to another process using hopen, how can it detect when that connection is closed by the remote end?

Solution

.z.pc:{[x] show "Connection closed. Handle: ",string x}
h:hopen 5001

later, once the remote end closes the connection:

"Connection closed. Handle: 5"

Explanation

.z.pc ("port close") fires automatically whenever a connection closes — deliberately, on a crash, or on a dropped network — passing the closed handle as its argument.

Defining it lets a process react immediately, rather than discovering the loss only when something later tries to use that handle. Typical uses: removing a stale handle from a subscriber list, logging the disconnect, or triggering a reconnect.