Problem with throwing exceptions in async handler functions of ASIO library
Dear Boost community,
We're having a problem using the ASIO class with multiple IO service
threads and throwing exceptions inside the handle_xxx functions, as used
by the async_xxx functions of the ASIO library.
It seems that when any error is thrown in a normal/non-handler function,
the try/catch block of the main program is able to catch the error.
However, when the handler functions are called (I would assume by the io
service, now running in a separate thread), the main function's
try/catch blocks never receive the execeptions, and a std::terminate()
is called automatically.
To demonstrate this, we've taken the HTTP Server 2 example from the ASIO
examples page and reduced its complexity. Below are the sources of the
example.
Basically, to duplicate the problem, run the main executable and then in
another terminal, simply "telnet 127.0.0.1 60000" to see the
std::terminate() being called, even though there are try/catch blocks in
the main function (main.cpp). Here is an output:
$ ./main
terminate called after throwing an instance of 'int'
Aborted
$
We've also taken the HTTP Server 2 example "as is" and added a "throw
123;" at the top of handle_accept in server.cpp and a "catch (...)"
block in posix_main.cpp. The same issue occurred here, which indicates
that the exceptions are not propagated through all the way (maybe
because the io service runs in a separate thread).
Does anyone have a suggestion on how we can throw exceptions inside the
handler functions of the async functions used? We'd like to handle all
our errors with custom exception classes (e.g. ExceptionAccept is thrown
if there is an error detected in handle_accept, which can then be
handled inside the main function of main.cpp).
We've compiled the sources below by typing:
g++ -I/usr/local/boost/include/boost-1_38 -L/usr/local/boost/lib
-lboost_thread-gcc41-mt-1_38 -lboost_system-gcc41-mt-1_38
io_service_pool.cpp server.cpp main.cpp -o main
where /usr/local/boost is the local boost installation path.
Many thanks in advance!
Kind regards,
Derik Thirion
main.cpp
--------
#include <iostream>
#include <string>
#include
m.. I haven't read everything, but it seems ok for me that exceptions are thrown and catched in the threads where io_service.run() was called. At least, so was the behavior in my program - I just wrapped io_service.run() with try-catch block.
Thanks. For me issue came in when one had a async_connect, e.g. inside the handle_resolve (called from io service thread as an async_resolve) was done. When a boost exception was thrown, it wasn't caught anywhere as the io_service::run was started directly in a thread, causing the program to terminate. I'm currently catching the errors in a wrapper thread to io_service's run and then propagating these exceptions back to the main thread and rethrow them there. On Fri, 2009-05-29 at 11:51 +0300, Roman Shmelev wrote:
m.. I haven't read everything, but it seems ok for me that exceptions are thrown and catched in the threads where io_service.run() was called. At least, so was the behavior in my program - I just wrapped io_service.run() with try-catch block. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
J.W.F. Thirion wrote:
We're having a problem using the ASIO class with multiple IO service threads and throwing exceptions inside the handle_xxx functions, as used by the async_xxx functions of the ASIO library.
It seems that when any error is thrown in a normal/non-handler function, the try/catch block of the main program is able to catch the error. However, when the handler functions are called (I would assume by the io service, now running in a separate thread), the main function's try/catch blocks never receive the execeptions, and a std::terminate() is called automatically.
Exceptions do not cross threads. You must catch the exception in the thread it is being thrown in, that is to say the thread running the io_service.
Hi everyone, I'm sure that this has been answered somewhere, but I was unable to find an answer: I'd like to set the stack size of a thread. Using pthreads, one can do this via pthread_attr_setstacksize, but I couldn't find a way of doing this via the boost thread library. Obviously this is critical for creating applications that start large numbers of threads. Is there a solution using the boost libraries? Kind regards, Derik Thirion
participants (3)
-
J.W.F. Thirion
-
Mathias Gaunard
-
Roman Shmelev