# Handling race condition signal and termination of workflow

**URL:** <https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039>\
**Category:** Community Support\
**Tags:** typescript-sdk\
**Created:** [April 26, 2023, 1:35pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039 "2023-04-26T13:35:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![irvenae](https://avatars.discourse-cdn.com/v4/letter/i/9de0a6/32.png) [@irvenae](https://community.temporal.io/u/irvenae)\
**Post date:** [April 26, 2023, 1:35pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039/1 "2023-04-26T13:35:39Z")

</div>

We are confused and worried about how signals are handled wrt awaits and if there is a guarantee that a signal is handled.

From the documentation it is said that a promise when sending a signal resolves when the signal is received by the server. What happens if this signal is received at the moment of completion of the workflow?

We have for example following code:

```auto
export async function restartFnOnItemUpdate(
  fn: () => Promise<void>
): Promise<void> {
  let updateScope = new CancellationScope({cancellable: true});

  setHandler(updateItemUrnSignal, () => {
    updateScope.cancel();
    updateScope = new CancellationScope({cancellable: true}); 
  });

  let notCompleted = true;
  while (notCompleted) {
    try {
      await updateScope.run(async () => {
        return fn();
      });
      notCompleted = false;
    } catch(error) {
      if(!isCancellation(error)) throw error;
    }
  }
}

```

The workflow calls this restartFnOnItemUpdate with the `fn` which consists of multiple activities. e.g.

```auto
export async function workflow() {
  await restartFnOnItemUpdate( () => {
    await activity1();
    await activity2();
  }):
};

```

How can we ensure the signal is always handled?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [April 26, 2023, 2:19pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039/2 "2023-04-26T14:19:52Z")

</div>

> What happens if this signal is received at the moment of completion of the workflow?

Temporal detects this condition and roll backs workflow state to allow signal handling.

> How can we ensure the signal is always handled?

Temporal guarantees that the signal handler is always called for a signal. You need to make sure that the signal is handled before completing the workflow.

---

<div class="post-metadata">

**Author:** ![irvenae](https://avatars.discourse-cdn.com/v4/letter/i/9de0a6/32.png) [@irvenae](https://community.temporal.io/u/irvenae)\
**Post date:** [April 26, 2023, 2:39pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039/3 "2023-04-26T14:39:26Z")

</div>

The question is then how can we ensure that the signal is handled?  
If I understand correctly the setHandler function could be called after the last await i.e. `await restartFnOnItemUpdate` meaning that the signal will have no influence.

Should we override the handler then with one which restarts the workflow? (continueAsNew)

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [April 26, 2023, 2:48pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039/4 "2023-04-26T14:48:00Z")

</div>

`setHandler` function is guaranteed to be called before the main workflow function is unblocked.

---

<div class="post-metadata">

**Author:** ![irvenae](https://avatars.discourse-cdn.com/v4/letter/i/9de0a6/32.png) [@irvenae](https://community.temporal.io/u/irvenae)\
**Post date:** [April 26, 2023, 3:07pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039/5 "2023-04-26T15:07:15Z")

</div>

![Screenshot 2023-04-26 at 17.01.35](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/1/120c7a6d70e229339256f823f9bf553374363d6f.jpeg)

In following screenshot we have a completion of a childWorkflow and at the same time the workflow got started to handle this completion a signal was received, this resulted in a start event to handle this signal.

Would in this situation the handler be called before the childWorkflow is handled (if we would map this to the example code above, the childWorkflow could be replaced by activity2)?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [April 26, 2023, 3:37pm UTC](https://community.temporal.io/t/handling-race-condition-signal-and-termination-of-workflow/8039/6 "2023-04-26T15:37:52Z")

</div>

The `WorkflowTaskFailed` happened exactly because workflow completion was requested and there was a signal that wasn’t handled. A workflow task retry was used to handle the signal.

> Would in this situation the handler be called before the childWorkflow is handled

Yes.
