Skip to content

Instantly share code, notes, and snippets.

@smashwilson
Created October 3, 2017 01:45
Show Gist options
  • Save smashwilson/ba51add3e2fd91c4b246686bfa659753 to your computer and use it in GitHub Desktop.
Save smashwilson/ba51add3e2fd91c4b246686bfa659753 to your computer and use it in GitHub Desktop.
Brainstorming possible Thread race conditions.

Thread states:

  • STOPPED

No state transitions are possible without the main thread initiating them. Sent messages are handled "offline." Depending on thread implementation, the offline command handler may:

  1. Alter private Thread state directly and acknowledge a command directly by producing an ack message on its output queue.
  2. Enqueue the command on the input queue and transition the Thread to STARTING.
  • STARTING

The Thread may transition to RUNNING at any time. Sent messages are enqueued on the input queue and no other action is taken.

  • RUNNING

The Thread may transition to STOPPING at any time. Sent messages are enqueued on the input queue and the thread is prompted to consume its queue at its next opportunity.

  • STOPPING

The Thread must transition to STOPPED shortly.

Any messages enqueued on the input queue in the time between the last queue consumption and the (atomic) transition to STOPPING will be shunted to the dead letter office.

Attempting to send new messages:

  1. Joins the Thread, transitioning it to STOPPED.
  2. Re-sends anything in the dead letter office and the new messages.

Problems

  • What if a message that would cause the Thread to start is stored in the dead letter office, and no other messages arrive to prompt the Thread to join and process it?*

  • Can a message enter the queue while the Thread is STARTING, but not be included it the initial batch of handled messages or trigger a wake-up?

  • Can the Thread be prompted to wake after it's prompted to begin STOPPING?

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