Possible deadlock in AsyncChannel::close

Please post support and help requests here.
Posts: 1
Joined: 27 Jun 2008, 16:20

Possible deadlock in AsyncChannel::close

Postby erikf » 29 Jun 2008, 05:59

A deadlock can occur in ~np~AsyncChannel::close~/np~ if the ~np~AsyncChannel's~/np~ thread is not waiting for a notification when _queue.wakeUpAll is called because the thread will never get notified to stop and will wait forever on its next call to _queue.waitDequeueNotification. This can occur if the ~np~AsyncChannel~/np~ is processing the last notification and the queue is empty when ~np~AsyncChannel::close~/np~ is called.

I'm able to reproduce the deadlock with the following code in an MFC application.

Code: Select all

void TestPocoLogger::Run()
   Poco::AutoPtr pPatternFormatter = new PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s:%q:%t");
   Poco::AutoPtr pFileChannel = new FileChannel("test.log");
   Poco::AutoPtr pAsyncChannel = new AsyncChannel(pFileChannel);
   Poco::AutoPtr pFormattingChannel = new FormattingChannel(pPatternFormatter, pAsyncChannel);

   Logger::create("FileLogger", pFormattingChannel, Message::PRIO_TRACE);
   Logger::get("FileLogger").trace("A longer log can increase the chance of getting a deadlock.");

The best solution I've found so far is to retry to wake the thread until the join succeeds.

Code: Select all

void AsyncChannel::close()
    if (_thread.isRunning())
        while (!_queue.empty()) Thread::sleep(100);
        while (!_thread.tryJoin(100));

Any other suggestions?


Posts: 1375
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Possible deadlock in AsyncChannel::close

Postby alex » 03 Jul 2008, 03:40

> Any other suggestions?

Looks OK to me (I could not reproduce the deadlock, though). Copied to SF patches. If nobody objects, I'll patch the SVN.

Posts: 1268
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Possible deadlock in AsyncChannel::close

Postby guenter » 03 Jul 2008, 08:32

The patch looks ok for me.

Return to “Support”

Who is online

Users browsing this forum: No registered users and 5 guests