Signal method invocation and workflow thread safety

How do the signal methods get invoked?

Based on the example here it looks like you don’t have to worry about thread safety.

Which thread handles the signals when there is an existing thread running the workflow code?

Workflow Multithreading

Workflow code can be multithreaded. Every time you call Async.function or Async.procedure the function passed to it is executed in its own thread. The exception is when a reference to an activity or child workflow method is passed. These are executed fully asynchronously.

To support deterministic execution of multithreaded code Temporal uses cooperative multithreading. Threads are executed one by one and are not preempted until they cease control to the framework. Control is ceased when one of the Temporal SDK functions is called. The most frequently used are:

  • Workflow.sleep
  • Future.get
  • Workflow.await

Due to this model, all standard synchronization and thread creation, and communication features are prohibited. When used they can cause thread blocking on non-framework API causing deadlocks.

Signals

Every signal is invoked in its own workflow thread. So blocking (using supported Workflow primitives) this thread doesn’t stop other signals from being delivered. As I mentioned earlier no explicit thread safety is needed as SDK already provides it.

I personally recommend not doing much work in signal handlers themselves. But use them to queue up work for the main workflow thread to process. But each application is different, so no hard rule here.