Skip to content

Conversation

@adrian-niculescu
Copy link
Contributor

@adrian-niculescu adrian-niculescu commented Nov 21, 2025

This PR ensures thread safety for Promises by preventing the global Promise object from being overwritten by user-space code or 3rd party libraries.

NativeScript relies on a custom Promise proxy implementation to ensure that callbacks are executed on the same thread where they were created. This is critical for iOS, where UI updates must happen on the main thread.

Some JavaScript libraries (e.g., Mocha) attempt to replace the global Promise with their own implementation or polyfill. If this happens, the runtime's thread marshalling logic is lost, leading to deadlocks, race conditions, or crashes when callbacks run on the wrong thread.

This change wraps globalThis in a Proxy during initialization to intercept and silently block any attempts to redefine or assign to the Promise property, strictly preserving the runtime's thread-safe implementation.

Note: This has been battle-tested since Nov 2020 in a project I work on.

@adrian-niculescu adrian-niculescu marked this pull request as draft November 21, 2025 11:47
@adrian-niculescu adrian-niculescu force-pushed the fix/promise-thread-safety branch from ac4eefa to 3128de6 Compare November 21, 2025 11:57
@adrian-niculescu adrian-niculescu force-pushed the fix/promise-thread-safety branch from 3128de6 to d8a3495 Compare November 21, 2025 12:00
@adrian-niculescu adrian-niculescu marked this pull request as ready for review November 21, 2025 12:00
@edusperoni
Copy link
Collaborator

edusperoni commented Nov 21, 2025

I'm not sure that preventing overwrite is the best idea, because some other things (like zone.js) override some parts of promise to intercept calls. I believe this might take too much of user agency over our globals, even if it's something that can potentially break an app due to incorrect polyfills.

Edit: additionally, making a global proxy is probably way too overkill, considering that's intercepting every single global call from now on (which also slows down everything on the global scope)

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