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

Memory corruption when using the server in multi-threaded mode. #1163

Closed githubguest03102023 closed 9 months ago

githubguest03102023 commented 9 months ago

When using the server in multi-threaded mode, we get exceptions of the following type: 2023-10-03 12:57:58 las-kernel-mercury *** Error in/usr/local/orglot/bin/kernel': double free or corruption (fasttop): 0x00007f128800a4c0 *** 2023-10-03 12:57:58 las-kernel-mercury ======= Backtrace: ========= 2023-10-03 12:57:58 las-kernel-mercury /lib64/libc.so.6(+0x81299)[0x7f1294f88299] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZNSs6assignERKSs+0x8c)[0x668cfc] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN22las_requests_processor20LasRequestsProcessor35mercury_api_terminal_properties_getERKSt8optionalIlERN8Pistache4Http14ResponseWriterE+0x228)[0x63ed78] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN3org12openapitools6server3api10DefaultApi43mercury_api_terminal_properties_get_handlerERKN8Pistache4Rest7RequestENS4_4Http14ResponseWriterE+0xc9)[0x577589] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel[0x57e5ca] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN8Pistache4Rest6Router5routeERKNS_4Http7RequestENS2_14ResponseWriterE+0xce7)[0x70c2a7] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN8Pistache4Rest7Private13RouterHandler9onRequestERKNS_4Http7RequestENS3_14ResponseWriterE+0x2f)[0x70c6af] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN8Pistache4Http7Handler7onInputEPKcmRKSt10shared_ptrINS_3Tcp4PeerEE+0x1ed)[0x6ca78d] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN8Pistache3Tcp9Transport14handleIncomingERKSt10shared_ptrINS0_4PeerEE+0x58)[0x6ee4b8] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN8Pistache3Tcp9Transport7onReadyERKNS_3Aio5FdSetE+0x115)[0x6f3465] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZNK8Pistache3Aio8SyncImpl9handleFdsESt6vectorINS_7Polling5EventESaIS4_EE+0x3d9)[0x721919] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZN8Pistache3Aio8SyncImpl7runOnceEv+0xb5)[0x721c05] 2023-10-03 12:57:58 las-kernel-mercury /usr/local/orglot/bin/kernel(_ZNSt6thread11_State_implINS_8_InvokerISt5tupleIJZN8Pistache3Aio9AsyncImpl6Worker3runEvEUlvE_EEEEE6_M_runEv+0xfa)[0x71fada] The json string is sent as a response using the following code: writer.headers().add("application/json"); writer.send(Pistache::Http::Code::Ok, json_resp);`

Is it possible to fix this problem?

dennisjenkins75 commented 9 months ago

Can you capture that in GDB and print a demangled stack trace?

githubguest03102023 commented 9 months ago

Probably yes. But it will take some time. Reproducing the error is very difficult.

kiplingw commented 9 months ago

@githubguest03102023, does it only happen in multithreaded mode? The reason I ask is I'd like to rule out a possible race condition in your own code.

githubguest03102023 commented 9 months ago

@githubguest03102023, does it only happen in multithreaded mode? The reason I ask is I'd like to rule out a possible race condition in your own code.

Yes, we get this error only in multi-threaded mode. I hope to get a core dump soon.

githubguest03102023 commented 9 months ago

I'm sorry for the trouble. The problem was the presence of global variables in a third party library and not related to the pistache. Thanks for your work.

kiplingw commented 9 months ago

Glad to hear, @githubguest03102023. If you later change your mind just let us know and we can always re-open this issue later.