Skip to content

Commit

Permalink
fetch.js cleanups / guard for infinite event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
noise64 committed Jun 11, 2024
1 parent a50422a commit c03dbed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions builtins/web/fetch/request-response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
#include "js/experimental/TypedData.h"
#pragma clang diagnostic pop

#define LOG(...) \
{ \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
fflush(stderr); \
}

namespace builtins::web::streams {

JSObject *NativeStreamSource::stream(JSObject *self) {
Expand Down Expand Up @@ -86,10 +93,13 @@ class BodyFutureTask final : public api::AsyncTask {

auto &chunk = read_res.unwrap();
if (chunk.done) {
LOG("chunk done");
RootedValue r(cx);
return Call(cx, controller, "close", HandleValueArray::empty(), &r);
}

LOG("chunk not done");

// We don't release control of chunk's data until after we've checked that
// the array buffer allocation has been successful, as that ensures that the
// return path frees chunk automatically when necessary.
Expand Down
15 changes: 9 additions & 6 deletions tests/integration/fetch/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ function asyncToSync(promise) {
done = true;
});

// NOTE: in golem the loop is needed
// while (!done) {
runEventLoopUntilInterest();
// }

let i = 0;
while (!done && i < 100) {
console.log("runEventLoop");
i += 1;
runEventLoop();
}

if (!done) {
let error = new Error("asyncToSync: illegal state: not done")
let error = new Error("asyncToSync: illegal state: not done");
console.error(error);
throw error
throw error;
}

if (!success) {
Expand Down

0 comments on commit c03dbed

Please sign in to comment.