fix random segfaults

This commit is contained in:
ading2210 2024-03-21 11:56:39 -07:00
parent 1bce63801d
commit 1ad301b26c
3 changed files with 31 additions and 12 deletions

View file

@ -1,5 +1,8 @@
# Libcurl.js Changelog: # Libcurl.js Changelog:
## v0.6.6 (3/20/24):
- Fix random segfaults due to an improper invocation of libcurl
## v0.6.5 (3/20/24): ## v0.6.5 (3/20/24):
- Update Wisp client and server - Update Wisp client and server
- Compile WolfSSL with greater site support - Compile WolfSSL with greater site support

View file

@ -51,6 +51,7 @@ if [[ "$*" == *"release"* ]]; then
echo "note: building with release optimizations" echo "note: building with release optimizations"
else else
COMPILER_OPTIONS="$COMPILER_OPTIONS --profiling -g" COMPILER_OPTIONS="$COMPILER_OPTIONS --profiling -g"
EMSCRIPTEN_OPTIONS="$EMSCRIPTEN_OPTIONS -sSTACK_OVERFLOW_CHECK=2 -sSAFE_HEAP=1"
fi fi
if [[ "$*" == *"single_file"* ]]; then if [[ "$*" == *"single_file"* ]]; then

View file

@ -7,6 +7,7 @@ class CurlSession {
this.active_requests = 0; this.active_requests = 0;
this.event_loop = null; this.event_loop = null;
this.requests_list = []; this.requests_list = [];
this.to_remove = [];
} }
assert_ready() { assert_ready() {
@ -54,16 +55,20 @@ class CurlSession {
return request_ptr; return request_ptr;
} }
remove_request(request_ptr) { remove_request_now(request_ptr) {
this.assert_ready();
_session_remove_request(this.session_ptr, request_ptr); _session_remove_request(this.session_ptr, request_ptr);
let request_index = this.requests_list.indexOf(request_ptr); let request_index = this.requests_list.indexOf(request_ptr);
if (request_index !== -1) { if (request_index !== -1) {
this.requests_list.splice(request_index, 1); this.requests_list.splice(request_index, 1);
} }
} }
//remove the request on the next iteration of the loop
remove_request(request_ptr) {
this.assert_ready();
this.to_remove.push(request_ptr);
}
start_request(request_ptr) { start_request(request_ptr) {
this.assert_ready(); this.assert_ready();
_session_add_request(this.session_ptr, request_ptr); _session_add_request(this.session_ptr, request_ptr);
@ -77,21 +82,31 @@ class CurlSession {
} }
this.event_loop = setInterval(() => { this.event_loop = setInterval(() => {
this.event_loop_func();
}, 0);
}
event_loop_func() {
let libcurl_active = _session_get_active(this.session_ptr); let libcurl_active = _session_get_active(this.session_ptr);
if (libcurl_active || this.active_requests) { if (libcurl_active || this.active_requests || this.to_remove) {
if (this.to_remove.length) {
for (let request_ptr of this.to_remove) {
this.remove_request_now(request_ptr);
}
this.to_remove = [];
}
_session_perform(this.session_ptr); _session_perform(this.session_ptr);
} }
else { else {
clearInterval(this.event_loop); clearInterval(this.event_loop);
this.event_loop = null; this.event_loop = null;
} }
}, 0);
} }
close() { close() {
this.assert_ready(); this.assert_ready();
for (let request_ptr of this.requests_list) { for (let request_ptr of this.requests_list) {
this.remove_request(request_ptr); this.remove_request_now(request_ptr);
} }
_session_cleanup(this.session_ptr); _session_cleanup(this.session_ptr);
this.session_ptr = null; this.session_ptr = null;