Commit Graph

34 Commits

Author SHA1 Message Date
Wladimir J. van der Laan c733ded07d http: Remove WaitExit from WorkQueue
This function, which waits for all threads to exit, is no longer needed
now that threads are joined instead.

Signed-off-by: Wladimir J. van der Laan <laanwj@gmail.com>

zcash: cherry picked from commit f94665466ed50e868c98b1a1c708ad5767727bb6
zcash: https://github.com/bitcoin/bitcoin/pull/12366
2021-04-12 17:15:22 -06:00
Wladimir J. van der Laan 0dcbfc9147 http: Join worker threads before deleting work queue
This prevents a potential race condition if control flow ends up in
`ShutdownHTTPServer` before the thread gets to `queue->Run()`,
deleting the work queue while workers are still going to use it.

Meant to fix #12362.

Signed-off-by: Wladimir J. van der Laan <laanwj@gmail.com>

zcash: cherry picked from commit b1c2370dde9ade180c638e5d9a4797f085322b5b
zcash: https://github.com/bitcoin/bitcoin/pull/12366
2021-04-12 17:15:22 -06:00
Cory Fields af7914242d rpc: further constrain the libevent workaround
The bug was introduced in 2.1.6-beta, versions before that don't need the
workaround.

zcash: currently, we build with libevent-2.1.12, so this fix is needed
zcash: cherry picked from commit 97932cd2689659addfbb58dc6148928b73af3bd0
zcash: https://github.com/bitcoin/bitcoin/pull/11593
2021-04-12 17:15:22 -06:00
Cory Fields 92d8f1d5cc rpc: work-around an upstream libevent bug
A rare race condition may trigger while awaiting the body of a message, see
upsteam commit 5ff8eb26371c4dc56f384b2de35bea2d87814779 for details.

This may fix some reported rpc hangs/crashes.

zcash: currently, we build with libevent-2.1.12, so this fix is needed
zcash: cherry picked from commit 6b58360f9b64eb0b680a662fdfd590e47f115f44
zcash: https://github.com/bitcoin/bitcoin/pull/11593
2021-04-12 17:15:22 -06:00
Matt Corallo 6cc2c45eb9 Access WorkQueue::running only within the cs lock.
This removes a "race" between Interrupt() and Run(), though it
should not effect any of our supported platforms.

zcash: cherry picked from commit 7b2d96b634f9fd283480caf3bece56138d0587e3
zcash: https://github.com/bitcoin/bitcoin/pull/9679
2021-04-01 14:29:40 -06:00
Jack Grigg a9f62bdda0 depends: Boost 1.74.0
- The old patch is no longer necessary because of this upstream fix:
    https://github.com/boostorg/build/pull/560

- Boost 1.72 removed a <deque> from an include, which exposed a missing
  include in src/httpserver.cpp.

- Boost 1.73 moved function placeholders into the boost::placeholders
  namespace.

- The new patch is a fix from just after Boost 1.74 was released, fixing
  a warning that was missed.
2020-10-05 19:48:46 +01:00
str4d 2fbc111efb
httpserver: Code style cleanups
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2020-10-01 13:46:21 +01:00
Cory Fields 860e6e600c httpserver: replace boost threads with std
along with mutex/condvar/bind/etc.

httpserver handles its own interruption, so there's no reason not to use std
threading.

While we're at it, may as well kill the BOOST_FOREACH's as well.
2020-09-30 12:48:35 +01:00
Cory Fields 97194f2abc httpserver: use a future rather than relying on boost's try_join_for 2020-09-30 12:48:35 +01:00
Wladimir J. van der Laan 26efd3aaf9 http: use std::move to move HTTPRequest into HTTPWorkItem
Thanks to Cory Fields for the idea.
2020-09-30 12:48:35 +01:00
Wladimir J. van der Laan a500df5561 http: Change boost::scoped_ptr to std::unique_ptr in HTTPRequest
No need for boost here.
2020-09-30 12:48:35 +01:00
Wladimir J. van der Laan 1f106fa803 http: Add log message when work queue is full
More useful error reporting.
2020-09-30 12:48:35 +01:00
Wladimir J. van der Laan fc9ce9e766 http: Do a pending c++11 simplification
Use std::unique_ptr for handling work items.

This makes the code more RAII and, as mentioned in the comment, is what
I planned when I wrote the code in the first place.
2020-09-30 12:48:35 +01:00
Wladimir J. van der Laan 23556e690e http: Restrict maximum size of request line + headers
Prevent memory exhaustion by sending lots of data.
Also add a test to `httpbasics.py`.

Closes #6425
2020-09-30 00:35:32 +01:00
zancas 8b6d9e55ba
Remove remaining instances of boost::function
These were not addressed by @practicalswift's upstream fix.
2020-02-04 20:08:25 -07:00
Daira Hopwood bc909a7a7f Replace http with https: in links to the MIT license.
Also change MIT/X11 to just MIT, since no distinction was intended.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2019-07-18 15:26:01 +01:00
Daniel Cousens 4519a766b6
move rpc* to rpc/ 2018-07-18 11:07:16 -06:00
Homu fb2f98e00b Auto merge of #2555 - jasondavies:fix-2554, r=str4d
Fix various thread assertion errors caused during shutdown.

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#6719
- bitcoin/bitcoin#6990
- bitcoin/bitcoin#8421
  - Second commit only in this PR
- bitcoin/bitcoin#11006

I've cherry-picked the relevant commits, along with a note in each commit referring to the original Bitcoin commit ID (and the Zcash issue numbers where applicable).  I've tested each issue with/without these patches applied.

Closes #2214, #2334, and #2554.
2017-10-23 15:15:14 -07:00
João Barbosa d3c8109b41 Improve shutdown process
Zcash: cherry-picked from commit 793667af1c31835e0eefcdd283930bb89cfeda8f
2017-10-23 14:14:45 +01:00
Jason Davies b30900a54b Replace "bitcoin" with "Zcash". 2017-10-05 18:10:15 +01:00
Cory Fields c98b91b7c5 httpserver: explicitly detach worker threads
When using std::thread in place of boost::thread, letting the threads destruct
results in a std::terminate. According to the docs, the same thing should be
be happening in later boost versions:
http://www.boost.org/doc/libs/1_55_0/doc/html/thread/thread_management.html#thread.thread_management.thread.destructor

I'm unsure why this hasn't blown up already, but explicitly detaching can't
hurt.

Zcash: cherry-picked from commit d3773ca9aeb0d2f12dc0c5a0726778050c8cb455
This fixes #2554 (zcash-cli stop during getblocktemplate long poll
causes 'Assertion `!pthread_mutex_unlock(&m)' failed.')
2017-08-01 20:24:27 +01:00
Wladimir J. van der Laan c7f77e28f0 http: speed up shutdown
This continues/fixes #6719.

`event_base_loopbreak` was not doing what I expected it to, at least in
libevent 2.0.21.
What I expected was that it sets a timeout, given that no other pending
events it would exit in N seconds. However, what it does was delay the
event loop exit with 10 seconds, even if nothing is pending.

Solve it in a different way: give the event loop thread time to exit
out of itself, and if it doesn't, send loopbreak.

This speeds up the RPC tests a lot, each exit incurred a 10 second
overhead, with this change there should be no shutdown overhead in the
common case and up to two seconds if the event loop is blocking.

As a bonus this breaks dependency on boost::thread_group, as the HTTP
server minds its own offspring.

Zcash: cherry-picked from commit a264c32e3321ae909ca59cb8ce8bf5d812dbc4e1
2017-08-01 20:24:27 +01:00
Wladimir J. van der Laan 2abe8ef721 http: Force-exit event loop after predefined time
This makes sure that the event loop eventually terminates, even if an
event (like an open timeout, or a hanging connection) happens to be
holding it up.

Zcash: cherry-picked from commit ec908d5f7aa9ad7e3487018e06a24cb6449cc58b
2017-08-01 20:24:27 +01:00
Wladimir J. van der Laan dbf7057f7a http: Wait for worker threads to exit
Add a WaitExit() call to http's WorkQueue to make it delete the work
queue only when all worker threads stopped.

This fixes a problem that was reproducable by pressing Ctrl-C during
AppInit2:
```
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:81: boost::condition_variable::~condition_variable(): Assertion `!ret' failed.
/usr/include/boost/thread/pthread/mutex.hpp:108: boost::mutex::~mutex(): Assertion `!posix::pthread_mutex_destroy(&m)' failed.
```

I was assuming that `threadGroup->join_all();` would always have been
called when entering the Shutdown(). However this is not the case in
bitcoind's AppInit2-non-zero-exit case "was left out intentionally
here".

Zcash: cherry-picked from commit de9de2de361ab1355b976f17371d73e36fe3bf56
Fixes #2334 and #2214.
2017-08-01 20:24:27 +01:00
Wladimir J. van der Laan c0fe29370a Make HTTP server shutdown more graceful
Shutting down the HTTP server currently breaks off all current requests.
This can create a race condition with RPC `stop` command, where the calling
process never receives confirmation.

This change removes the listening sockets on shutdown so that no new
requests can come in, but no longer breaks off requests in progress.

Meant to fix bitcoin/#6717.

Zcash: cherry-picked from commit 5e0c22135600fe36811da3b78216efc61ba765fb
2017-08-01 20:24:27 +01:00
paveljanik 17694e4bcf
[TRIVIAL] Fix typo: exactmath -> exactmatch
... but not yet in trivial tree
2017-03-24 18:42:45 +13:00
Wladimir J. van der Laan 08c581940a
auto_ptr → unique_ptr
Change the few occurrences of the deprecated `auto_ptr` to c++11 `unique_ptr`.
Silences the deprecation warnings.

Also add a missing `std::` for consistency.
2017-03-24 09:03:59 +13:00
Gregory Maxwell 599d2c4034
Avoid a compile error on hosts with libevent too old for EVENT_LOG_WARN.
This uses _EVENT_LOG_WARN instead, which appears to be defined in the
 old versions of libevent that I have on some systems.
2017-03-24 09:03:59 +13:00
Wladimir J. van der Laan 89bccddcd8
rpc: Split option -rpctimeout into -rpcservertimeout and -rpcclienttimeout
The two timeouts for the server and client, are essentially different:

- In the case of the server it should be a lower value to avoid clients
clogging up connection slots

- In the case of the client it should be a high value to accomedate slow
  responses from the server, for example for slow queries or when the
  lock is contended

Split the options into `-rpcservertimeout` and `-rpcclienttimeout` with
respective defaults of 30 and 900.
2017-03-24 09:03:59 +13:00
Wladimir J. van der Laan a659991a2c
http: Disable libevent debug logging, if not explicitly enabled
Add a option "-debug=libevent" to enable libevent debugging for troubleshooting.
Libevent logging is redirected to our own log.
2017-03-24 09:03:59 +13:00
Wladimir J. van der Laan 116503c0b8
Fix race condition between starting HTTP server thread and setting EventBase()
Split StartHTTPServer into InitHTTPServer and StartHTTPServer to give
clients a window to register their handlers without race conditions.

Thanks @ajweiss for figuring this out.
2017-03-24 09:03:59 +13:00
Wladimir J. van der Laan 9fb5b94e64
Document options for new HTTP/RPC server in --help 2017-03-24 09:03:59 +13:00
Wladimir J. van der Laan 858afa1a4a
Implement RPCTimerHandler for Qt RPC console
Implement RPCTimerHandler for Qt RPC console, so that `walletpassphrase`
works with GUI and `-server=0`.

Also simplify HTTPEvent-related code by using boost::function directly.

Zcash: QT changes ommitted
2017-03-24 09:03:59 +13:00
Wladimir J. van der Laan afd64f76ea
evhttpd implementation
- *Replace usage of boost::asio with [libevent2](http://libevent.org/)*.
boost::asio is not part of C++11, so unlike other boost there is no
forwards-compatibility reason to stick with it. Together with #4738 (convert
json_spirit to UniValue), this rids Bitcoin Core of the worst offenders with
regard to compile-time slowness.

- *Replace spit-and-duct-tape http server with evhttp*. Front-end http handling
is handled by libevent, a work queue (with configurable depth and parallelism)
is used to handle application requests.

- *Wrap HTTP request in C++ class*; this makes the application code mostly
HTTP-server-neutral

- *Refactor RPC to move all http-specific code to a separate file*.
Theoreticaly this can allow building without HTTP server but with another RPC
backend, e.g. Qt's debug console (currently not implemented) or future RPC
mechanisms people may want to use.

- *HTTP dispatch mechanism*; services (e.g., RPC, REST) register which URL
paths they want to handle.

By using a proven, high-performance asynchronous networking library (also used
by Tor) and HTTP server, problems such as #5674, #5655, #344 should be avoided.

What works? bitcoind, bitcoin-cli, bitcoin-qt. Unit tests and RPC/REST tests
pass. The aim for now is everything but SSL support.

Configuration options:

- `-rpcthreads`: repurposed as "number of  work handler threads". Still
defaults to 4.

- `-rpcworkqueue`: maximum depth of work queue. When this is reached, new
requests will return a 500 Internal Error.

- `-rpctimeout`: inactivity time, in seconds, after which to disconnect a
client.

- `-debug=http`: low-level http activity logging
2017-03-24 09:03:59 +13:00