Skip to content

DownloadHandlerSseBase: Switch to Unity main thread before processing#4

Open
xavier-ateo wants to merge 1 commit intoSov3rain:masterfrom
xavier-ateo:fix-download-handler-out-of-sync-with-main-thread
Open

DownloadHandlerSseBase: Switch to Unity main thread before processing#4
xavier-ateo wants to merge 1 commit intoSov3rain:masterfrom
xavier-ateo:fix-download-handler-out-of-sync-with-main-thread

Conversation

@xavier-ateo
Copy link
Contributor

This code executed outside of the Unity main thread context in my project, which caused certain realtime subscription callbacks to crash the editor. The fix captures Unity's main thread SynchronizationContext and uses it to send all OnNewLineReceived callbacks back to the main thread.

I am not certain if this is the only place where code executes outside of the main thread context, or if there are other places in the codebase that need to be synchronized.

@Sov3rain
Copy link
Owner

Sov3rain commented Nov 13, 2025

Thanks for raising this! I don't understand how it could happen frankly. Custom download scripts implementing DownloadHandlerScript are called from the main thread, see: https://docs.unity3d.com/2020.1/Documentation//Manual/UnityWebRequest-CreatingDownloadHandlers.html

Maybe the culprit is elsewhere. The SseClient use this class for example.

@Sov3rain
Copy link
Owner

Hey @xavier-ateo , do you have a stack trace or a log of the crash? Or at least a reproductible scenario?

@xavier-ateo
Copy link
Contributor Author

I agree. I was also under the impression that this should not happen. However, after I deployed this change, my editor stopped crashing.

It’s a bit of a difficult scenario to debug, because I can’t capture the crash in the debugger, and the stack trace doesn’t provide many details. I also haven’t had the time to do a deeper investigation yet.

When I find some time, I’ll try to create a reproducible scenario and test it on different machines and different Unity versions.

If you want, you can close this issue for now, and I’ll get back to you once I have more information.

@Sov3rain
Copy link
Owner

I'll keep this PR open as a reminder (mainly for myself 😅). Does the crash happen in a build or just in the editor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants