Section I – SCXML > SCXML > Termination Bottom of Page
| Genesys 8.0 SCXML Technical Reference
< Runtime Core Elements >

## Termination

#### Application-Driven Termination

There are several ways an application can terminate a session:

- `<final>` as a Child of the `<scxml>` Element Termination — The session is terminated after the `<onexit>` logic is run. A done.xxx event is then fired by the orchestration platform for that session. The xxx part of the event is different depending on how the session was started. In the case of `<invoke>` initation, the xxx is "invoke" plus the invokeid from the original request. In the case of `<session:start>` and web service interface initiation, it is the word "scxml". This is done regardless of the type of session it is (parent or child), but there are exceptions (for example, in the case of `<invoke>` initiation, when the invoking session terminates the invoked session). In addition, data can be sent with this done event. See the SCXML Element Extensions section for details.

- `<invoke>`-Related Termination (Initiated by the Invoking Session) — When the invoking session ends (`<final>`) or leaves the invoking state (`<transition target=new>`, the orchestration platform will perform the cancel operation (internal) on the invoked session. When cancelling an invoked session, the platform will send the cancel.invoke.invokeid event to the invoked session first. This will be an external event. After consuming this event, the invoked session will exit at the end of the next microstep. When the invoked session exits, it will execute the `<onexit>` handlers for all active states. Note: a done event is not fired in this case, because the invoking session is not interested in the event.

- `<session:terminate>` Termination — This action terminates a session from another session. This will result in the orchestration platform terminating the selected session (same termination process as with `<invoke>`-related termination (initiated by the invoking session)), but it will send the appropriate done.xxx event to only the initiating session. See the terminate action definition for more details.

- TerminateSession Web Service Termination — This action is the same as the `<session:terminate>` action, except it is performed by a 3rd party application via a web service interface.

#### Platform–Driven Termination

In addition to the application-driven ways for terminating a session, the platform may encounter session hung conditions in which it will need to terminate the corresponding sessions. The following are the hung conditions which need to be handled by the platform:

- The termination cancel operation has not finished in a reasonable time frame.
- The `<final>` processing for an application has not finished in a reasonable time frame.
- A session has been idle for a given time period (no events or processing)
- A session has executed too many of the same type of SCXML element (`<transition>`).
- A session spends too long executing an element (`<script>`, `<queue:submit>`, for example)

Because of this, the platform must have the necessary set of platform configuration options to handle these conditions. In most cases, it is configuring a count or a time period.

The general process for handling these conditions is to do the following:

- When the condition is encountered, the platform will send the (internal) session.terminating event, which will give the session the ablilty to gracefully terminate before the platform terminates it.
- If the session does not take action in a timely manner (during the platform-wide configured time periodof something like 1 or 2 seconds), then the platform will forcibly terminate the session. But unlike other cancel operations (`<invoke>`, for example) it will not execute the `<onexit>` handlers. It will simply terminate the session.

The following diagram shows the general processing that will occur when these conditions are detected:

Section I – SCXML > SCXML > Termination Top of Page
| Genesys 8.0 SCXML Technical Reference
< Runtime Core Elements >