Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(native): the stack-overflow check follows the thread that calls in

esengine1142e910fdee
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 46f9e9ca6..ea92f9c4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,21 @@ published separately; it ships inside the editor.
## [Unreleased]
+### Fixed
+
+- **Coming back to a running game no longer kills it, sometimes.** On Android,
+ recreating the activity — leave with Back and reopen, rotate, toggle dark mode —
+ keeps the process and the booted engine but hands every later JS call to a new
+ thread. QuickJS measures stack overflow from a stack top it records once, on the
+ thread that created the runtime; when the new thread's stack lands lower than
+ that mark minus the budget, every call — however flat — throws "Maximum call
+ stack size exceeded", forever. Whether it lands there is address-space luck,
+ which is why the same build died one smoke run in forty, first at
+ `es_onNativeVisibility` (the first call after a resume) and then on every frame.
+ The check is re-anchored to the calling thread at each host→JS entry now — the
+ API's own answer to a runtime outliving its thread, at the cost of one stack
+ pointer read.
+
## [0.41.0] - 2026-08-03
The editor's own agent is the headline, and this is the release where a user can
diff --git a/native/host/Runtime.cpp b/native/host/Runtime.cpp
index 0540c72da..e080a5aa3 100644
--- a/native/host/Runtime.cpp
+++ b/native/host/Runtime.cpp
@@ -333,13 +333,27 @@ void logJsError(JSContext* ctx, const char* where) {
JS_FreeValue(ctx, e);
}
+// QuickJS detects JS stack overflow by comparing the current C stack pointer
+// against a top it records ONCE, at JS_NewRuntime. The runtime outlives the
+// thread that made it: on Android an activity recreate keeps the process (and
+// the booted engine) but hands every later JS call to a NEW glue thread, whose
+// stack ASLR may map more than the 1MB budget below the recorded top — at which
+// point every call, however flat, throws "Maximum call stack size exceeded"
+// forever. Re-anchoring at each host→JS entry is the API's own answer to a
+// runtime crossing threads, and costs one stack-pointer read.
+void jsEntry(HostState& h) {
+ JS_UpdateStackTop(h.rt);
+}
+
void evalJs(HostState& h, const char* src, const char* name) {
+ jsEntry(h);
JSValue r = JS_Eval(h.js, src, strlen(src), name, JS_EVAL_TYPE_GLOBAL);
if (JS_IsException(r)) logJsError(h.js, name);
JS_FreeValue(h.js, r);
}
void callJs(HostState& h, const char* fn, int argc, JSValue* argv) {
+ jsEntry(h);
JSValue global = JS_GetGlobalObject(h.js);
JSValue f = JS_GetPropertyStr(h.js, global, fn);
if (JS_IsFunction(h.js, f)) {
@@ -352,6 +366,7 @@ void callJs(HostState& h, const char* fn, int argc, JSValue* argv) {
}
void pumpJs(HostState& h) {
+ jsEntry(h);
pumpJobs(h);
pumpTimers(h);
pumpJobs(h);
diff --git a/native/host/Runtime.hpp b/native/host/Runtime.hpp
index 7d5a89f6a..2edae7e72 100644
--- a/native/host/Runtime.hpp
+++ b/native/host/Runtime.hpp
@@ -129,6 +129,12 @@ void bindGlobal(HostState& h, JSValue global, const char* name, JSCFunction* fn,
/** Report a pending JS exception through the platform log, with its stack. */
void logJsError(JSContext* ctx, const char* where);
+/** Re-anchor QuickJS's stack-overflow check to the calling thread's stack.
+ * Must open every host→JS entry: the runtime outlives the thread that made it
+ * (Android activity recreate), and a stale anchor turns every call into a
+ * false "Maximum call stack size exceeded" — permanently. */
+void jsEntry(HostState& h);
+
/** Evaluate a global script; exceptions are logged, not propagated. */
void evalJs(HostState& h, const char* src, const char* name);
diff --git a/native/host/bindings/NetBindings.cpp b/native/host/bindings/NetBindings.cpp
index 8c64186de..0ac8de4fb 100644
--- a/native/host/bindings/NetBindings.cpp
+++ b/native/host/bindings/NetBindings.cpp
@@ -129,6 +129,7 @@ JSValue makeFetchResult(HostState& h, const FetchResult& r) {
} // namespace
void drainFetches(HostState& h) {
+ jsEntry(h);
std::queue<FetchResult> done;
{ std::lock_guard<std::mutex> lk(g_fetchMutex); done.swap(g_fetchQueue); }
while (!done.empty()) {
diff --git a/native/host/bindings/TextEditorBindings.cpp b/native/host/bindings/TextEditorBindings.cpp
index 120b4d977..ebbb9b5f1 100644
--- a/native/host/bindings/TextEditorBindings.cpp
+++ b/native/host/bindings/TextEditorBindings.cpp
@@ -116,6 +116,7 @@ void drainTextEditor(HostState& h) {
std::queue<EditorPush> pending;
{ std::lock_guard<std::mutex> lk(g_mutex); pending.swap(g_queue); }
if (pending.empty()) return;
+ jsEntry(h);
JSValue global = JS_GetGlobalObject(h.js);
JSValue fn = JS_GetPropertyStr(h.js, global, "es_onNativeTextEditor");