Skip to content

Fix deadlock when using EJS templates in watch mode #1421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

robertjpayne
Copy link
Contributor

On macOS 15.4.1 if you are using EJS templates in watch mode Sourcery deadlocks after a few template changes because the of work JavaScriptCore attempts to do on the main thread when cleaning up old resources.

Since JavaScriptCore is not easily debuggable its hard to say exactly what is happening but it appears that JSContext()! without any parameters allocates a new JSVirtualMachine each time and when the context gets deinit'd (whenever a template changes) the JSVirtualMachine also deinit's. During that deinit it takes over the main thread and deadlocks sourcery file watchers.

This change uses dedicated VMs for the entire duration of the Sourcery process. The downside is that memory usage may go up over time in watch mode, but it wont deadlock and subsequent parsing/executing of templates may speed up as it can utilise a byte code cache etc…

Creates and re-use a static JSVirtualMachine when rendering EJS templates so watch mode doesn't break when old JSContext deallocate and deadlock the main thread.
@krzysztofzablocki krzysztofzablocki merged commit d5a605b into krzysztofzablocki:master Jun 4, 2025
2 checks passed
@krzysztofzablocki
Copy link
Owner

thanks @robertjpayne

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