---
title: "Nested Invocations"
url: https://docs.zeroc.com/ice/3.8/runtime/threading-model/nested-invocations
language: cpp
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Nested Invocations

Programming language: C++. Other languages: [C#](https://docs.zeroc.com/ice/3.8/runtime/threading-model/nested-invocations/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/threading-model/nested-invocations/java.md), [Python](https://docs.zeroc.com/ice/3.8/runtime/threading-model/nested-invocations/python.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/threading-model/nested-invocations/swift.md).

A *nested invocation* is an invocation made from a dispatch or from the callback of an asynchronous invocation.

## Deadlocks with Nested Invocations

Nested invocations are common, but they can deadlock. To avoid deadlocks, do not hold a shared resource, such as a mutex or a thread of an [Ice thread pool](https://docs.zeroc.com/ice/3.8/runtime/threading-model/thread-pools/cpp.md), while waiting. The rest of this page shows the common case, a nested invocation made from a dispatch; the same rule applies to a callback.

A synchronous dispatch holds the thread that executes it, by default a thread of the server thread pool, until it returns, and a synchronous twoway invocation blocks its thread until the response arrives. A synchronous invocation made from a synchronous dispatch therefore holds a dispatch thread while it waits. If the invocation depends, directly or indirectly, on another dispatch from the same thread pool, the two can deadlock:

![The client calls opA on Server A. While dispatching opA, Server A calls opB on Server B. Server A's only dispatch thread waits for opB to return, so Server A cannot dispatch Server B's callback.](https://docs.zeroc.com/images/ice/3.8/nested-invocations/nested-deadlock.svg)

*Nested invocation deadlock.*

In this example, all the calls are synchronous, and both servers use the default server thread pool, which has a single thread. Server A's thread is blocked waiting for opB to complete, but Server B cannot complete opB until Server A dispatches its callback. Neither invocation can complete.

A larger server thread pool avoids this deadlock only while fewer nested invocations are pending than the pool has threads. To avoid exhausting the dispatch threads, release the dispatch thread while the nested invocation is pending: implement the outer operation with asynchronous dispatch (AMD), make the nested invocation asynchronously, and complete the dispatch when the nested invocation completes. Making the nested invocation asynchronously and then waiting for its result in the dispatch thread still holds that thread. With asynchronous dispatch, you can also make the synchronous invocation from a thread of your own instead of an Ice thread pool thread.

> **Note**
>
> A oneway invocation does not wait for a response from the server. However, a synchronous oneway invocation can block while the Ice runtime establishes a connection or writes the request to the connection. To avoid blocking a dispatch thread or a thread holding a mutex, make an asynchronous oneway invocation.

## See Also

- [Thread Pools](https://docs.zeroc.com/ice/3.8/runtime/threading-model/thread-pools/cpp.md)
- [Object Adapter Thread Pools](https://docs.zeroc.com/ice/3.8/runtime/threading-model/object-adapter-thread-pools.md)
- [Thread Pool Design Considerations](https://docs.zeroc.com/ice/3.8/runtime/threading-model/thread-pool-design-considerations/cpp.md)
- [Oneway Invocations](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/oneway-invocations/cpp.md)
