Skip to content

Commit

Permalink
Merge pull request #256 from ktock/fixhttpfail
Browse files Browse the repository at this point in the history
js: fix worker to make sure to wait for request completion
  • Loading branch information
ktock authored Apr 9, 2024
2 parents f9f5782 + e659fd9 commit 4c6f123
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/wasi-browser/htdocs/worker-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function wasiHackSocket(wasi, listenfd, connfd) {
}
var _fd_prestat_get = wasi.wasiImport.fd_prestat_get;
wasi.wasiImport.fd_prestat_get = (fd, prestat_ptr) => {
if ((fd == listenfd) || (fd <= connfd)){ // reserve socket-related fds
if ((fd == listenfd) || (fd == connfd)){ // reserve socket-related fds
let buffer = new DataView(wasi.inst.exports.memory.buffer);
buffer.setUint8(prestat_ptr, 1);
return 0;
Expand Down
75 changes: 52 additions & 23 deletions extras/runcontainerjs/src/web/runcontainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ function connect(name, shared, toNet) {
if (timeoutHandler) {
clearTimeout(timeoutHandler);
timeoutHandler = null;
} else {
Atomics.store(streamCtrl, 0, 1);
Atomics.notify(streamCtrl, 0);
}
break;
case "recv-is-readable":
Expand Down Expand Up @@ -166,17 +163,14 @@ function connect(name, shared, toNet) {
Atomics.notify(toNetNotify, 0);
clearTimeout(timeoutHandler);
timeoutHandler = null;
// streamStatus[0] = 0;
Atomics.store(streamCtrl, 0, 1);
Atomics.notify(streamCtrl, 0);
}, 0.01);
}
pollBuf();
return;
} else {
streamStatus[0] = 0; // timeout
Atomics.store(toNetNotify, 0, -1);
Atomics.notify(toNetNotify, 0);
}
streamStatus[0] = 0; // timeout
Atomics.store(toNetNotify, 0, -1);
Atomics.notify(toNetNotify, 0);
}
break;
case "http_send":
Expand Down Expand Up @@ -223,31 +217,66 @@ function connect(name, shared, toNet) {
connObj.request.body = connObj.reqBodybuf;
}
fetch(connObj.address, connObj.request).then((resp) => {
var headers = {};
for (const key of resp.headers.keys()) {
headers[key] = resp.headers.get(key);
}
connObj.response = new TextEncoder().encode(JSON.stringify({
bodyUsed: resp.bodyUsed,
headers: headers,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
type: resp.type,
url: resp.url
})),
connObj.done = false;
connObj.respBodybuf = new Uint8Array(0);
if (resp.ok) {
resp.arrayBuffer().then((data) => {
var headers = {};
for (const key of resp.headers.keys()) {
if (data.byteLength > 0) {
if (key == "content-encoding") {
continue;
}
if (key == "content-length") {
headers[key] = data.byteLength.toString();
continue;
}
}
headers[key] = resp.headers.get(key);
}
connObj.response = new TextEncoder().encode(JSON.stringify({
bodyUsed: resp.bodyUsed,
headers: headers,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
type: resp.type,
url: resp.url
}));
connObj.respBodybuf = new Uint8Array(data);
connObj.done = true;
}).catch((error) => {
var headers = {};
for (const key of resp.headers.keys()) {
headers[key] = resp.headers.get(key);
}
connObj.response = new TextEncoder().encode(JSON.stringify({
bodyUsed: resp.bodyUsed,
headers: headers,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
type: resp.type,
url: resp.url
}));
connObj.respBodybuf = new Uint8Array(0);
connObj.done = true;
console.log("failed to fetch body: " + error);
});
} else {
var headers = {};
for (const key of resp.headers.keys()) {
headers[key] = resp.headers.get(key);
}
connObj.response = new TextEncoder().encode(JSON.stringify({
bodyUsed: resp.bodyUsed,
headers: headers,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
type: resp.type,
url: resp.url
}));
connObj.done = true;
}
}).catch((error) => {
Expand Down
2 changes: 2 additions & 0 deletions extras/runcontainerjs/src/web/stack-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,11 @@ function sockWaitForReadable(timeout){
streamCtrl[0] = 0;
Atomics.store(toNetNotify, 0, 0);
postMessage({type: "recv-is-readable", timeout: timeout});
Atomics.wait(streamCtrl, 0, 0);
Atomics.wait(toNetNotify, 0, 0);
var res = Atomics.load(toNetNotify, 0);

streamCtrl[0] = 0;
postMessage({type: "recv-is-readable-cancel"});
Atomics.wait(streamCtrl, 0, 0);

Expand Down
2 changes: 1 addition & 1 deletion extras/runcontainerjs/src/web/wasi-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function wasiHackSocket(wasi, listenfd, connfd, sockAccept, sockSend, soc
}
var _fd_prestat_get = wasi.wasiImport.fd_prestat_get;
wasi.wasiImport.fd_prestat_get = (fd, prestat_ptr) => {
if ((fd == listenfd) || (fd <= connfd)){ // reserve socket-related fds
if ((fd == listenfd) || (fd == connfd)){ // reserve socket-related fds
let buffer = new DataView(wasi.inst.exports.memory.buffer);
buffer.setUint8(prestat_ptr, 1);
return 0;
Expand Down
6 changes: 3 additions & 3 deletions extras/runcontainerjs/src/web/worker-util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Event, EventType, Subscription, SubscriptionClock, SubscriptionFdReadWrite, SubscriptionU, wasiHackSocket } from './wasi-util';
import { WASI, PreopenDirectory } from "@bjorn3/browser_wasi_shim";
import { WASI, PreopenDirectory, File } from "@bjorn3/browser_wasi_shim";
import * as wasitype from "@bjorn3/browser_wasi_shim";

export function startContainer(info, cargs, ttyClient) {
Expand Down Expand Up @@ -423,12 +423,12 @@ function getCertDir(cert) {
var o = ret.fd_obj;
ret.fd_obj.fd_pread = (view8, iovs, offset) => {
var old_offset = o.file_pos;
var r = o.fd_seek(offset, WHENCE_SET);
var r = o.fd_seek(offset, wasitype.wasi.WHENCE_SET);
if (r.ret != 0) {
return { ret: -1, nread: 0 };
}
var read_ret = o.fd_read(view8, iovs);
r = o.fd_seek(old_offset, WHENCE_SET);
r = o.fd_seek(old_offset, wasitype.wasi.WHENCE_SET);
if (r.ret != 0) {
return { ret: -1, nread: 0 };
}
Expand Down

0 comments on commit 4c6f123

Please sign in to comment.