You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Task.Run(() =>sub.PublishAsync(data)).Wait(); // Still DEADLOCK!
154
+
};
155
+
```
156
+
157
+
**Why does this deadlock?**
158
+
159
+
The SDK dispatches events on the thread pool but uses internal synchronization to maintain message order and state consistency. When you block an event handler waiting for an SDK async method to complete, you create a circular wait:
160
+
161
+
1. Event handler blocks waiting for `PublishAsync()` to complete
162
+
2.`PublishAsync()` needs to acquire internal locks held by the event dispatcher
163
+
3. Event dispatcher can't release locks until your handler completes
164
+
4.**Deadlock!** ⚠️
165
+
166
+
**⚠️ Blocking on Non-SDK Operations**
167
+
168
+
```csharp
169
+
subscription.Publication+= (sender, e) =>
170
+
{
171
+
// ⚠️ Safe from deadlock, but blocks a thread pool thread (impacts performance)
172
+
File.WriteAllText("messages.txt", message); // Blocks thread pool
Blocking on non-SDK operations (file I/O, database calls, HTTP requests to other services) won't deadlock, but it ties up a thread pool thread, reducing overall application performance. Use `async`/`await` whenever possible.
181
+
182
+
**Exception Handling in Async Event Handlers**
183
+
184
+
Exceptions in `async void` event handlers **cannot be caught by the SDK** and will crash your application if unhandled:
185
+
186
+
```csharp
187
+
// ❌ BAD: Unhandled exceptions will crash your app
188
+
subscription.Publication+=async (sender, e) =>
189
+
{
190
+
awaitriskyOperation(); // If this throws, app crashes!
191
+
};
192
+
193
+
// ✅ GOOD: Always wrap async event handlers in try-catch
0 commit comments