Skip to content

Commit

Permalink
Merge pull request bellard#40 from openwebf/fix/20230313_promise_asyn…
Browse files Browse the repository at this point in the history
…c_crash

fix: pending promise shouldn't release by gc and should free after runtime shutdown
  • Loading branch information
ErosZy authored Mar 13, 2023
2 parents 2c6fbc9 + 78f61f9 commit de6baa0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/builtins/js-promise.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void js_promise_mark(JSRuntime *rt, JSValueConst val,
struct list_head *el;
int i;

if (!s)
if (!s || (rt->state != JS_RUNTIME_STATE_SHUTDOWN && s->promise_state == JS_PROMISE_PENDING))
return;
for(i = 0; i < 2; i++) {
list_for_each(el, &s->promise_reactions[i]) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,9 @@ void JS_FreeRuntime(JSRuntime* rt) {
struct list_head *el, *el1;
int i;

if (rt->state == JS_RUNTIME_STATE_SHUTDOWN)
return;
rt->state = JS_RUNTIME_STATE_SHUTDOWN;
JS_FreeValueRT(rt, rt->current_exception);

list_for_each_safe(el, el1, &rt->job_list) {
Expand Down Expand Up @@ -3063,6 +3066,7 @@ JSRuntime* JS_NewRuntime2(const JSMallocFunctions* mf, void* opaque) {
JS_UpdateStackTop(rt);

rt->current_exception = JS_NULL;
rt->state = JS_RUNTIME_STATE_INIT;

return rt;
fail:
Expand Down
6 changes: 6 additions & 0 deletions src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ typedef struct {
} JSNumericOperations;
#endif

typedef enum {
JS_RUNTIME_STATE_INIT,
JS_RUNTIME_STATE_SHUTDOWN,
} JSRuntimeState;

struct JSRuntime {
JSMallocFunctions mf;
JSMallocState malloc_state;
Expand Down Expand Up @@ -226,6 +231,7 @@ struct JSRuntime {
uint32_t operator_count;
#endif
void *user_opaque;
JSRuntimeState state;
};

struct JSClass {
Expand Down
14 changes: 14 additions & 0 deletions tests/test_promise_gc_crash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
async function createTask() {
return Promise.resolve().then(function () {
new Uint8Array(1000000)
})
}

run()
async function run() {
let fn = (v) => { console.log(v.length); }
let done = (v) => fn(v)
createTask().then(done)
const p = new Promise(() => { })
await p
}

0 comments on commit de6baa0

Please sign in to comment.