Chapter 6
Who owns the socket?
Choose context, thread/process ownership, transport and shutdown boundaries.
Prerequisite: What completed when send returned?. Course map →
The situation
Two handlers share one REQ socket to save allocations. Replies become difficult to attribute, and shutdown sometimes blocks. The design has mixed ownership, concurrency and messaging state.
A context is not a shared conversation
A ZeroMQ context is an object your program uses to create sockets. A thread is a path of execution inside a process. Threads in one process can share the context. For the socket types in these labs, have one thread create, use and close each socket.
Send a message to the thread that owns a socket when another thread needs it to do work. Check the documentation for your socket type and library version before choosing a different ownership rule.
Choose transport deliberately
An inproc address connects sockets created by the same context. An IPC endpoint connects sockets on one machine through an operating-system address, such as a filesystem path. TCP carries messages through the network stack. Each transport has its own connection setup, queues and access rules, even when the same message goes through it.
The temporary Linux guest supports both local filesystem addresses and TCP loopback. Loopback keeps a connection inside the guest. The labs use example peers there. Test routing, firewall rules, identity checks and failures on the network your service will use.
Socket ownership and transport
Illustrative case.
Compare inproc and TCP loopback. Which can carry a message between two processes in this guest?
| Boundary | Useful evidence | Remaining question |
|---|---|---|
| inproc | Same-context message path | No cross-context or network claim |
| TCP loopback | Real guest sockets/processes | External reachability is separate |
| Shared socket | Local state and ownership | Choose a single owner for these types |
| Shutdown | Drain/close/terminate | Bound waiting and recover pending work |
Design ownership of shutdown
Stop admitting new work, decide which pending operations must finish, drain under a deadline, then close sockets and terminate their context. Set a time limit on linger, the time ZeroMQ allows queued sends to finish during shutdown. Give the thread that owns the socket a way to stop waiting for a message. Check separately whether the receiver committed the requested work.
An infinite wait can turn a disconnected peer into an application shutdown hang. With LINGER=0, closing a socket discards messages still waiting to be sent. Decide how your application will recover that pending work.
Test the lifecycle you deploy
Exercise startup ordering, reconnects, cancellation, owner failure and process exit. Use explicit readiness messages or observable handshake states where the scenario needs them. Fixed sleeps conceal assumptions about scheduling and machine speed.
The labs disclose their synchronization. Some drive multiple sockets serially in one Python process; the TCP and proxy investigations use separate processes. Neither a serial fixture nor inproc behavior establishes a multi-host scheduling or throughput result.
Check your understanding
Two threads take turns sending on the same ordinary REQ socket. A lock prevents their sends from overlapping. How do the threads know which caller should get each reply?
Show hints
Hint 1
What operation does REQ expect immediately after a send?
Hint 2
Who is responsible for routing the reply to its caller?
Show explanation
Give the socket one owning thread. That thread completes each send and receive in order, then returns the result to its caller.
ReferencesPrimary sources for this chapter’s design case
Use these guides to check the framework or protocol contract discussed in this chapter. Moving “stable” or “current” guides can describe a different release; check the version used by your deployment. These readings do not execute the distributed frameworks they compare.
PyZMQ guidance
ZeroMQ context