Opened 6 years ago

Last modified 6 years ago

#11410 new Bug Report - Hang/Deadlock

backend hang: mpeg recordings with vbi sometimes hang at finish

Reported by: rd.mora@… Owned by: danielk
Priority: minor Milestone: unknown
Component: MythTV - Recording Version: 0.26-fixes
Severity: medium Keywords: mpegrecorder vbi hang
Cc: Ticket locked: no

Description (last modified by Raymond Wagner)

Witnessed mpeg recordings with vbi occasionally hanging at finish (causing the backend in general to hang) and under gdb saw:

Thread 3 (Thread 0x7fbd69ffb700 (LWP 8322)):
#0  0x00007fbd8bf7a61c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00007fbd8c207699 in QWaitCondition::wait(QMutex*, unsigned long) () from /usr/lib64/libQtCore.so.4
#2  0x00007fbd8c206824 in QThread::wait(unsigned long) () from /usr/lib64/libQtCore.so.4
#3  0x00007fbd9293f9bc in MpegRecorder::run (this=0x7fbd6c224d00) at mpegrecorder.cpp:1124
#4  0x00007fbd91b9ec07 in MThread::run (this=0x7fbd6c9f35a0) at mthread.cpp:319
#5  0x00007fbd8c206775 in ?? () from /usr/lib64/libQtCore.so.4
#6  0x00007fbd8bf767b6 in start_thread () from /lib64/libpthread.so.0
#7  0x00007fbd8b5399cd in clone () from /lib64/libc.so.6
#8  0x0000000000000000 in ?? ()

Thread 2 (Thread 0x7fbd697fa700 (LWP 8323)):
#0  0x00007fbd8bf7d76d in read () from /lib64/libpthread.so.0
#1  0x00007fbd9292263c in V4LRecorder::RunVBIDevice (this=0x7fbd6c224d00) at v4lrecorder.cpp:308
#2  0x00007fbd9291aeb2 in VBIThread::run (this=0x1111d40) at v4lrecorder.h:82
#3  0x00007fbd8c206775 in ?? () from /usr/lib64/libQtCore.so.4
#4  0x00007fbd8bf767b6 in start_thread () from /lib64/libpthread.so.0
#5  0x00007fbd8b5399cd in clone () from /lib64/libc.so.6
#6  0x0000000000000000 in ?? ()

Looking at mpegrecorder.cpp:

1113	    LOG(VB_RECORD, LOG_INFO, LOC + "run finishing up");
1114	
1115	    StopEncoding();
1116	
1117	    {
1118	        QMutexLocker locker(&pauseLock);
1119	        request_helper = false;
1120	    }
1121	
1122	    if (vbi_thread)
1123	    {
1124	        vbi_thread->wait();
1125	        delete vbi_thread;
1126	        vbi_thread = NULL;
1127	        CloseVBIDevice();
1128	    }
1129	
1130	    FinishRecording();

Line 1119 signals the vbi thread to stop reading and exit, however, the encoder is signaled to stop *before* this on line 1115. Thus it is possible for the vbi thread to return from select, try to read data from the vbi fd and then block on the read because the vbi fd is no longer returning data (has been stopped).

Simple solution seems to be to swap the order, i.e. signal vbi thread to stop reading before stopping the encoder. I have no idea if there are any unforeseen consequences to this though..... comments?

diff --git a/mythtv/libs/libmythtv/mpegrecorder.cpp b/mythtv/libs/libmythtv/mpegrecorder.cpp
index b158513..35644af 100644
--- a/mythtv/libs/libmythtv/mpegrecorder.cpp
+++ b/mythtv/libs/libmythtv/mpegrecorder.cpp
@@ -1112,13 +1112,13 @@ void MpegRecorder::run(void)

     LOG(VB_RECORD, LOG_INFO, LOC + "run finishing up");

-    StopEncoding();
-
     {
         QMutexLocker locker(&pauseLock);
         request_helper = false;
     }

+    StopEncoding();
+
     if (vbi_thread)
     {
         vbi_thread->wait();
lines 1-20/20 (END)

Change History (2)

comment:1 Changed 6 years ago by Raymond Wagner

Description: modified (diff)
Priority: majorminor
Severity: highmedium

comment:2 Changed 6 years ago by rd.mora@…

Hmm, that diff isn't going to work - it is still possible for the vbi thread to be blocked in a read before we signal it and then stop the encoding. Even moving the StopEncoding?() to after the vbi_thread->wait() won't work - the vbi_thread could be blocked on a read and because there is now nothing reading data from the mpeg FD the driver buffers full up and it stops writing to the VBI FD. The best solution seems to be to put the VBI FD into non-blocking mode, for which the changes required actually seem pretty minimal, i.e.:

diff --git a/mythtv/libs/libmythtv/v4lrecorder.cpp b/mythtv/libs/libmythtv/v4lrecorder.cpp
index a16214f..79239ad 100644
--- a/mythtv/libs/libmythtv/v4lrecorder.cpp
+++ b/mythtv/libs/libmythtv/v4lrecorder.cpp
@@ -129,7 +129,7 @@ int V4LRecorder::OpenVBIDevice(void)
     }
     else if (VBIMode::NTSC_CC == vbimode)
     {
-        fd = open(vbidev.constData(), O_RDONLY/*|O_NONBLOCK*/);
+        fd = open(vbidev.constData(), O_RDONLY|O_NONBLOCK);
     }
     else
     {
@@ -326,7 +326,7 @@ void V4LRecorder::RunVBIDevice(void)
                 }
                 ptr = buf;
             }
-            else if (ret < 0)
+            else if ((ret < 0) && (EAGAIN != errno) && (EINTR != errno))
             {
                 LOG(VB_GENERAL, LOG_ERR, LOC + "Reading VBI data" + ENO);
             }

NOTE: This diff does not handle PAL VBI, just NTSC!

I'll update the ticket if I run into any trouble with this change....

Note: See TracTickets for help on using tickets.