Skip to content

Commit 64ac663

Browse files
committed
Add re-entrancy guard tip
1 parent 3279637 commit 64ac663

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/core/diagnostics/eventsource-collect-and-view-traces.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,23 @@ this isn't required. Following is an example `EventListener` implementation that
283283
> - **Avoid Complex Operations**: Refrain from performing complex operations within the callback that might acquire additional locks. For example, during some event callbacks, attempting to use File or Console APIs may encounter issues. Instead, you can update an in-memory datastructure or add some information about the event to a queue. If more processing is needed it can be done from a separate thread after the callback has already returned.
284284
> - **Minimize Lock Duration**: Ensure that any locks acquired within the callback are not held for extended periods.
285285
> - **Use Non-blocking APIs**: Prefer using non-blocking APIs within the callback to avoid potential deadlocks.
286+
> - **Implement Re-entrancy Guard**: Use a re-entrancy guard to prevent infinite recursion. For example:
287+
>
288+
> ```csharp
289+
> [ThreadStatic] private static bool t_insideCallback;
290+
>
291+
> public void OnEventWritten(...)
292+
> {
293+
> if (t_insideCallback) return; // if our callback triggered the event to occur recursively
294+
> // exit now to avoid infinite recursion
295+
> try
296+
> {
297+
> t_insideCallback = true;
298+
> // do callback work
299+
> }
300+
> finally
301+
> {
302+
> t_insideCallback = false;
303+
> }
304+
> }
305+
> ```

0 commit comments

Comments
 (0)