From d2548271e97fd3dc89f25d903c9dd7f3ed76cb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Date: Sun, 16 Sep 2007 18:04:15 +0000 Subject: [PATCH] Fix race condition of file io --- protocolfile.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/protocolfile.c b/protocolfile.c index 1f21423..57ef8c7 100644 --- a/protocolfile.c +++ b/protocolfile.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolfile.c,v 1.4 2007/09/16 17:31:38 rahrenbe Exp $ + * $Id: protocolfile.c,v 1.5 2007/09/16 18:04:15 ajhseppa Exp $ */ #include @@ -78,8 +78,18 @@ int cIptvProtocolFile::Read(unsigned char* *BufferAddr) // Rewind if EOF if (feof(fileStream)) rewind(fileStream); - // Read the file stream - return fread(readBuffer, sizeof(unsigned char), readBufferLen, fileStream); + + // Sleep before reading the file stream to prevent aggressive busy looping + cCondWait::SleepMs(50); + + // This check is to prevent a race condition where file may be switched off + // during the sleep and buffers are disposed. Check here that the plugin is + // still active before accessing the buffers + if (fileActive) + return fread(readBuffer, sizeof(unsigned char), readBufferLen, + fileStream); + else + return -1; } bool cIptvProtocolFile::Open(void)