mthom / scryer-prolog

A modern Prolog implementation written mostly in Rust.
BSD 3-Clause "New" or "Revised" License
1.93k stars 116 forks source link

Bug in `http/http_server` library, bad configuration causes server to crash without cleaning up #2422

Open lachrimae opened 2 weeks ago

lachrimae commented 2 weeks ago

System specs

OS: Pop 22.04 jammy Kernel: x86_64 Linux 6.8.0-76060800daily20240311-generic CPU: 13th Gen Intel Core i5-1335U @ 12x 4.6GHz

Reproducing the issue

Start with a Prolog module

% main.pl
:- use_module(library(http/http_server)).

handler(Request, Response) :-
  http_headers(Response, "invalid data for headers").

Then run

$ RUST_BACKTRACE=full scryer-prolog ./main.pl
?- http_listen(8080, [post(/, handler)]).

In a separate terminal, run

$ curl -d "some arbitrary post body" localhost:8080

Expected outcome

The server should respond HTTP 500 with some kind of error message and continue to serve new HTTP requests.

What actually happens next

The curl session will hang until you issue the ^C kill command, suggesting that no HTTP response has been sent.

Simultaneously, the scryer-prolog session will evaluate to false. and quit out from evaluating http_listen. However, it is impossible to use ^C to exit the process at this point. If instead of trying to exit the process, you re-evaluate http_listen(8080, [post(/, handler)]). then the Rust runtime will issue this panic message:

?- http_listen(8080, [post(/, handler)]).
Listening at 0.0.0.0:8080
thread 'tokio-runtime-worker' panicked at /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001
f/warp-0.3.5/src/server.rs:213:27:
error binding to 0.0.0.0:8080: error creating server listener: Address already in use (os error 98)
stack backtrace:
   0:     0x56aa4f1255ac - std::backtrace_rs::backtrace::libunwind::trace::he43a6a3949163f8c
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/../../ba
cktrace/src/backtrace/libunwind.rs:93:5
   1:     0x56aa4f1255ac - std::backtrace_rs::backtrace::trace_unsynchronized::h50db52ca99f692e7
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/../../ba
cktrace/src/backtrace/mod.rs:66:5
   2:     0x56aa4f1255ac - std::sys_common::backtrace::_print_fmt::hd37d595f2ceb2d3c
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/sys_comm
on/backtrace.rs:67:5
   3:     0x56aa4f1255ac - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::f
mt::h678bbcf9da6d7d75
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/sys_comm
on/backtrace.rs:44:22
   4:     0x56aa4f1547ec - core::fmt::rt::Argument::fmt::h3a159adc080a6fc9
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/fmt/rt.
rs:138:9
   5:     0x56aa4f1547ec - core::fmt::write::hb8eaf5a8e45a738e
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/fmt/mod
.rs:1094:21
   6:     0x56aa4f120a2e - std::io::Write::write_fmt::h9663fe36b2ee08f9
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/io/mod.r
s:1714:15
   7:     0x56aa4f125394 - std::sys_common::backtrace::_print::hcd4834796ee88ad2
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/sys_comm
on/backtrace.rs:47:5
   8:     0x56aa4f125394 - std::sys_common::backtrace::print::h1360e9450e4f922a
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/sys_comm
on/backtrace.rs:34:9
   9:     0x56aa4f126a43 - std::panicking::default_hook::{{closure}}::h2609fa95cd5ab1f4
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panickin
g.rs:270:22
  10:     0x56aa4f12675c - std::panicking::default_hook::h6d75f5747cab6e8d
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panickin
g.rs:290:9
  11:     0x56aa4f126fc9 - std::panicking::rust_panic_with_hook::h57e78470c47c84de
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panickin
g.rs:707:13
  12:     0x56aa4f126ec7 - std::panicking::begin_panic_handler::{{closure}}::h3dfd2453cf356ecb
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panickin
g.rs:599:13
  13:     0x56aa4f125ad6 - std::sys_common::backtrace::__rust_end_short_backtrace::hdb177d43678e4d7e
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/sys_comm
on/backtrace.rs:170:18
  14:     0x56aa4f126c12 - rust_begin_unwind
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panickin
g.rs:595:5
  15:     0x56aa4eb0fb63 - core::panicking::panic_fmt::hd1e971d8d7c78e0e
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicki
ng.rs:67:14
  16:     0x56aa4ed10494 - warp::server::Server<F>::bind_ephemeral::hecf89346eae9aa9c
  17:     0x56aa4edcaa39 - scryer_prolog::machine::system_calls::<impl scryer_prolog::machine::Machine>::h
ttp_listen::{{closure}}::h803691b73131d337
  18:     0x56aa4edbef8e - tokio::runtime::task::core::Core<T,S>::poll::ha36dd6d4603f497e
  19:     0x56aa4edc1604 - tokio::runtime::task::harness::Harness<T,S>::poll::h92adc8cd064d0bce
  20:     0x56aa4f0bad1c - tokio::runtime::scheduler::multi_thread::worker::Context::run_task::h91ae26f636
8f3767
  21:     0x56aa4f0b72ec - tokio::runtime::context::set_scheduler::h9350979f142fbcef
  22:     0x56aa4f0a69ef - tokio::runtime::context::runtime::enter_runtime::h19ceb716524abeb5
  23:     0x56aa4f0bab88 - tokio::runtime::scheduler::multi_thread::worker::run::h5f7880e973ac8674
  24:     0x56aa4f0a96da - tokio::runtime::task::core::Core<T,S>::poll::h62e9d7cefbc7ae34
  25:     0x56aa4f0a019e - tokio::runtime::task::harness::Harness<T,S>::poll::h40f7e9dd8e55108f
  26:     0x56aa4f0a9fb6 - std::sys_common::backtrace::__rust_begin_short_backtrace::h8098b49f9db346a6
  27:     0x56aa4f0a2049 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h5cafb942610c50e2
  28:     0x56aa4f12cd75 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::haadd
4e5af2ab0d62
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/alloc/src/boxed.
rs:2007:9
  29:     0x56aa4f12cd75 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he4ba
1fb09c16d807
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/alloc/src/boxed.
rs:2007:9
  30:     0x56aa4f12cd75 - std::sys::unix::thread::Thread::new::thread_start::he524ecf4b47bee95
                               at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/sys/unix
/thread.rs:108:17
  31:     0x77cf24494ac3 - start_thread
                               at ./nptl/pthread_create.c:442:8
  32:     0x77cf24526850 - __GI___clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
  33:                0x0 - <unknown>
aarroyoc commented 2 weeks ago

The problem of not cleaning the server resources can be traced back to https://github.com/mthom/scryer-prolog/pull/1880/files#diff-a21834d24cd9843d97005c64c4a0568ae1f5df4043d26f528159f215ac2941dfR5225, where it was disabled to enable the library mode of Scryer Prolog. Some refactoring is required.