pistacheio / pistache

A high-performance REST toolkit written in C++
https://pistacheio.github.io/pistache/
Apache License 2.0
3.12k stars 688 forks source link

Data race in Pistache::Http::Experimental::Client #1159

Closed tyler92 closed 11 months ago

tyler92 commented 11 months ago

It's reproduced with the unit test http_server_test.multiple_client_with_requests_to_multithreaded_server.

WARNING: ThreadSanitizer: data race (pid=3415914)
  Write of size 8 at 0x7ba800000468 by thread T3 (mutexes: write M0):
    #0 close <null> 
    #1 Pistache::Http::Experimental::Connection::close() src/client/client.cc:624:9 
    #2 Pistache::Http::Experimental::ConnectionPool::shutdown() src/client/client.cc:881:27 
    #3 Pistache::Http::Experimental::Client::shutdown() src/client/client.cc:993:14 
    #4 clientLogicFunc(unsigned long, std::string const&, int, int) tests/http_server_test.cc:241:12 

  Previous read of size 8 at 0x7ba800000468 by thread T7:
    #0 recv <null> 
    #1 Pistache::Http::Experimental::Transport::handleIncoming(std::shared_ptr<Pistache::Http::Experimental::Connection>) src/client/client.cc:499:35 
    #2 Pistache::Http::Experimental::Transport::handleReadableEntry(Pistache::Aio::FdSet::Entry const&) src/client/client.cc:420:17 
    #3 Pistache::Http::Experimental::Transport::onReady(Pistache::Aio::FdSet const&) src/client/client.cc:255:17 
    #4 Pistache::Aio::SyncImpl::handleFds(std::vector<Pistache::Polling::Event>) const src/common/reactor.cc:205:34 
    #5 Pistache::Aio::SyncImpl::runOnce() src/common/reactor.cc:165:21 
    #6 Pistache::Aio::SyncImpl::run() src/common/reactor.cc:177:17 
    #7 Pistache::Aio::AsyncImpl::Worker::run()::'lambda'()::operator()() const src/common/reactor.cc:515:27 

The full report: tsan.log

tyler92 commented 11 months ago

And the same test sometimes failed with the following error (without thread sanitizer):

[==========] Running 1 test from 1 test suite.
[----------] 1 test from http_server_test
[ RUN      ] http_server_test.multiple_client_with_requests_to_multithreaded_server
[test] [7f3d5c450a00] Trying to run server...
[server] [7f3d5c450a00] Init Hello handler with 0 seconds delay
[test] [7f3d5c450a00] Server address: localhost:41011
run_http_server_test: /home/misha/github.com/pistacheio/pistache/src/client/client.cc:977: Pistache::Http::Experimental::Client::~Client(): Assertion `stopProcessPequestsQueues == true && "You must explicitly call shutdown method of Client object"' failed.
run_http_server_test: /home/misha/github.com/pistacheio/pistache/src/client/client.cc:977: Pistache::Http::Experimental::Client::~Client(): Assertion `stopProcessPequestsQueues == true && "You must explicitly call shutdown method of Client object"' failed.
Aborted (core dumped)

or

[==========] Running 1 test from 1 test suite.
[----------] 1 test from http_server_test
[ RUN      ] http_server_test.multiple_client_with_requests_to_multithreaded_server
[test] [7f5f1645ea00] Trying to run server...
[server] [7f5f1645ea00] Init Hello handler with 0 seconds delay
[test] [7f5f1645ea00] Server address: localhost:33679
[client] [7f5f0c2c3640] [0] Response: OK, body: `Hello, World!`
[client] [7f5f0bac2640] [2] Response: OK, body: `Hello, World!`
[client] [7f5f0bac2640] [1] Response: OK, body: `Hello, World!`
[client] [7f5f0bac2640] [0] Response: OK, body: `Hello, World!`
[client] [7f5f0c2c3640] [1] Response: OK, body: `Hello, World!`
[client] [7f5f116ec640] resolves: 2, rejects: 0, request timeout: 0 seconds, wait: 6 seconds
[client] [7f5f0f3e0640] resolves: 3, rejects: 0, request timeout: 0 seconds, wait: 6 seconds
/home/misha/github.com/pistacheio/pistache/tests/http_server_test.cc:339: Failure
Expected equality of these values:
  res1
    Which is: 2
  FIRST_CLIENT_REQUEST_SIZE
    Which is: 4
[  FAILED  ] http_server_test.multiple_client_with_requests_to_multithreaded_server (6009 ms)
tyler92 commented 11 months ago

I think this is a duplication of https://github.com/pistacheio/pistache/issues/842