Skip to content
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

Uncatchable error shouldn't be caught in promise #810

Open
laishere opened this issue Jan 10, 2025 · 0 comments
Open

Uncatchable error shouldn't be caught in promise #810

laishere opened this issue Jan 10, 2025 · 0 comments

Comments

@laishere
Copy link
Contributor

Original issue I posted here bellard/quickjs#341

Test Code

#include <gtest/gtest.h>
#include <quickjs.h>

#define MAX_VAL 100

static int timeout_interrupt_handler(JSRuntime *rt, void *opaque) {
    int *time = (int *)opaque;
    if (*time == MAX_VAL) {
        std::cout << "Timeout" << std::endl;
    }
    if (*time <= MAX_VAL) {
        *time += 1;
    }
    return *time > MAX_VAL;
}

TEST(QuickJS, timeout_interrupt) {
    const char *code = 
"(async function() { \
    const loop = async () => { \
        await Promise.resolve(); \
        while (true) {} \
    }; \
    while (true) { \
        await loop().catch(() => {}); \
    } \
})();";

    JSRuntime *rt = JS_NewRuntime();
    JSContext *ctx = JS_NewContext(rt);
    int time = 0;
    JS_SetInterruptHandler(rt, timeout_interrupt_handler, &time);
    JSValue ret = JS_Eval(ctx, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL);
    EXPECT_FALSE(JS_IsException(ret));
    JS_FreeValue(ctx, ret);
    EXPECT_TRUE(JS_IsJobPending(rt));
    int r = 0;
    while (JS_IsJobPending(rt)) {
        r = JS_ExecutePendingJob(rt, &ctx);
    }
    EXPECT_GT(time, MAX_VAL);
    EXPECT_FALSE(JS_IsJobPending(rt));
    EXPECT_EQ(r, -1);
    EXPECT_TRUE(JS_HasException(ctx));
    JS_FreeContext(ctx);
    JS_FreeRuntime(rt);
}

Expected
It should end when timeout occurs.

Actual
It never ends.

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

No branches or pull requests

1 participant