diff --git a/CHANGELOG.md b/CHANGELOG.md index caf4398..612eeec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 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): - Update Wisp client and server - Compile WolfSSL with greater site support diff --git a/client/build.sh b/client/build.sh index ac0b268..1bc0b66 100755 --- a/client/build.sh +++ b/client/build.sh @@ -51,6 +51,7 @@ if [[ "$*" == *"release"* ]]; then echo "note: building with release optimizations" else COMPILER_OPTIONS="$COMPILER_OPTIONS --profiling -g" + EMSCRIPTEN_OPTIONS="$EMSCRIPTEN_OPTIONS -sSTACK_OVERFLOW_CHECK=2 -sSAFE_HEAP=1" fi if [[ "$*" == *"single_file"* ]]; then diff --git a/client/javascript/session.js b/client/javascript/session.js index bac98aa..bd17e64 100644 --- a/client/javascript/session.js +++ b/client/javascript/session.js @@ -7,6 +7,7 @@ class CurlSession { this.active_requests = 0; this.event_loop = null; this.requests_list = []; + this.to_remove = []; } assert_ready() { @@ -54,16 +55,20 @@ class CurlSession { return request_ptr; } - remove_request(request_ptr) { - this.assert_ready(); + remove_request_now(request_ptr) { _session_remove_request(this.session_ptr, request_ptr); - let request_index = this.requests_list.indexOf(request_ptr); if (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) { this.assert_ready(); _session_add_request(this.session_ptr, request_ptr); @@ -77,21 +82,31 @@ class CurlSession { } this.event_loop = setInterval(() => { - let libcurl_active = _session_get_active(this.session_ptr); - if (libcurl_active || this.active_requests) { - _session_perform(this.session_ptr); - } - else { - clearInterval(this.event_loop); - this.event_loop = null; - } + this.event_loop_func(); }, 0); } + event_loop_func() { + let libcurl_active = _session_get_active(this.session_ptr); + 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); + } + else { + clearInterval(this.event_loop); + this.event_loop = null; + } + } + close() { this.assert_ready(); for (let request_ptr of this.requests_list) { - this.remove_request(request_ptr); + this.remove_request_now(request_ptr); } _session_cleanup(this.session_ptr); this.session_ptr = null;