About weird behavior with boost.asio 1.63 on linux 2.6.15
Dear All:
I have been using boost over 10+ years. The asio library work
quite well along with document.
Today I get a problem:
My development environment:
gcc 4.7.3 OABI
Linux 2.6.15
boost 1.63
The code looks like below:
void myTask(const char *text)
{
sleep(5);
/*for(;;)
{
//sleep(1);
}*/
cout<<"text is "<
On 12/20/23 18:27, Scott Zhang via Boost wrote:
Dear All: I have been using boost over 10+ years. The asio library work quite well along with document. Today I get a problem: My development environment: gcc 4.7.3 OABI Linux 2.6.15 boost 1.63
The code looks like below: void myTask(const char *text) { sleep(5); /*for(;;) { //sleep(1); }*/ cout<<"text is "<
I get 2 problems: 1. If I run program as above, the mytask get executed correctly, but as soon as it returns, the program get "illegal instruction", and program dies. If I call myTask directly, then everything is ok. So what's wrong with io_service.post
2. If I run ioservice within thread using threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioService) Then after myTask executed, I get "Killed", using strace says program get SIGKILL.
As the test code is so simple, I can't tell where goes wrong.
I also noticed, if I don't call io_service.post, everything looks ok. And the threads, in linux ps, shows as many processes.
I reread the document of asio 1.63, it says had beed tested against linux 2.4 and 2.6 kernel. so it should be ok.
Were you able to collect the stacktrace of the crash? You should be able to do it by running your code under gdb. Does this reproduce with optimizations disabled?
The code looks like below: void myTask(const char *text) { sleep(5); /*for(;;) { //sleep(1); }*/ cout<<"text is "<
Does the problem happen if you call any function other than sleep? Doing a blocking sleep in an async task is something to avoid in general. It may be causing interferences with asio's internal scheduler.
Hello. Thanks for replying.
No. There is no crash, just illegal instruction. And it happens after
the function run over.
After recheck the document,
https://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/using.html
BOOST_ASIO_DISABLE_THREADS
Explicitly disables Boost.Asio's threading support, independent of
whether or not Boost as a whole supports threads.
I find by -DBOOST_ASIO_DISABLE_THREADS the code works fine.
Still don't know why
On Thu, Dec 21, 2023 at 2:35 AM Ruben Perez
The code looks like below: void myTask(const char *text) { sleep(5); /*for(;;) { //sleep(1); }*/ cout<<"text is "<
Does the problem happen if you call any function other than sleep? Doing a blocking sleep in an async task is something to avoid in general. It may be causing interferences with asio's internal scheduler.
BOOST_ASIO_DISABLE_THREADS
Explicitly disables Boost.Asio's threading support, independent of whether or not Boost as a whole supports threads.
I find by -DBOOST_ASIO_DISABLE_THREADS the code works fine.
From the Linux man pages: https://man7.org/linux/man-pages/man3/sleep.3.html sleep is listed as MT-unsafe.
From this thread: https://unix.stackexchange.com/questions/528362/sleep-system-call-on-linux-i... This behaviour is used as a workaround for a bug that is still present in the Linux version you're using. Calling a MT-unsafe function from a multi-threaded environment can yield to behaviour like the one you're describing. Can you please replace the sleep call by a nanosleep (https://man7.org/linux/man-pages/man2/nanosleep.2.html), which is MT-safe (or a console print statement), and see if the behaviour reproduces?
Still don't know why
On Thu, Dec 21, 2023 at 2:35 AM Ruben Perez
wrote: The code looks like below: void myTask(const char *text) { sleep(5); /*for(;;) { //sleep(1); }*/ cout<<"text is "<
Does the problem happen if you call any function other than sleep? Doing a blocking sleep in an async task is something to avoid in general. It may be causing interferences with asio's internal scheduler.
Hi Ruben.
Thanks for replying.
The problem is not within sleep. I used sleep to identify where the
code got illegal instruction then got killed.
The problem, as I described, is that without
DBOOST_ASIO_DISABLE_THREADS, it happened after
the function ran over. Instead of returning peacefully, got "illegal
instruction" error.
I think the problem is because the old kernel and gcc I used, the
device was made in 2009 so the system is quite old. Besides this , if
I use '-fPIC' which gcc, the program crashed either.
On Thu, Dec 21, 2023 at 10:39 PM Ruben Perez
BOOST_ASIO_DISABLE_THREADS
Explicitly disables Boost.Asio's threading support, independent of whether or not Boost as a whole supports threads.
I find by -DBOOST_ASIO_DISABLE_THREADS the code works fine.
From the Linux man pages: https://man7.org/linux/man-pages/man3/sleep.3.html sleep is listed as MT-unsafe.
From this thread: https://unix.stackexchange.com/questions/528362/sleep-system-call-on-linux-i... This behaviour is used as a workaround for a bug that is still present in the Linux version you're using. Calling a MT-unsafe function from a multi-threaded environment can yield to behaviour like the one you're describing. Can you please replace the sleep call by a nanosleep (https://man7.org/linux/man-pages/man2/nanosleep.2.html), which is MT-safe (or a console print statement), and see if the behaviour reproduces?
Still don't know why
On Thu, Dec 21, 2023 at 2:35 AM Ruben Perez
wrote: The code looks like below: void myTask(const char *text) { sleep(5); /*for(;;) { //sleep(1); }*/ cout<<"text is "<
Does the problem happen if you call any function other than sleep? Doing a blocking sleep in an async task is something to avoid in general. It may be causing interferences with asio's internal scheduler.
participants (3)
-
Andrey Semashev
-
Ruben Perez
-
Scott Zhang