Skip to content

Instantly share code, notes, and snippets.

@hiteshjasani
Last active November 20, 2015 13:09
Show Gist options
  • Save hiteshjasani/0e1b2869d62f0ee7856e to your computer and use it in GitHub Desktop.
Save hiteshjasani/0e1b2869d62f0ee7856e to your computer and use it in GitHub Desktop.
Terminate context hang

Main Thread:

  • create context (zmq/context 1)
  • create workers and pass them the context
  • start the worker threads
  • addShutdownHook
    • shutdown the worker threads
    • join the worker threads
    • sleep 2s ;; without this line the terminate context sometimes hangs, with it so far it has never hung
    • terminate the context (.term context) ;; sometimes hangs here

Worker Thread:

  • Use the context to create some sockets
    • sockets are created using with-open so they get auto closed
    • sockets have their linger value set to 100ms
  • Use context to create poller (zmq/poller context 1)
  • Register main receive socket with poller (zmq/register poller client :pollin)
  • Loop, poll for data on main receive socket, timeout then loop
  • If the shutdown flag is set, then
    • unregister main receive socket from poller (zmq/unregister poller client)
    • close all the sockets that we created - actually get autoclosed from with-open
  • done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment