Skip to content

Commit

Permalink
fix(qgisserver): add select call to avoid to wait on recv when socket…
Browse files Browse the repository at this point in the history
… is closed
  • Loading branch information
benoitdm-oslandia committed Jan 3, 2025
1 parent 47b04b4 commit 55810cd
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/server/qgsfcgiserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,52 @@ void QgsSocketMonitoringThread::run()

mShouldStop.store( false );
char c;

fd_set setOptions;
FD_ZERO( &setOptions ); // clear the set
FD_SET( mIpcFd, &setOptions ); // add our file descriptor to the set

struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 50000; // max 50ms of timeout for select

while ( !mShouldStop.load() )
{
const ssize_t x = recv( mIpcFd, &c, 1, MSG_PEEK | MSG_DONTWAIT ); // see https://stackoverflow.com/a/12402596
if ( x != 0 )
{
// Ie. we are still connected but we have an 'error' as there is nothing to read
QgsDebugMsgLevel( QStringLiteral( "FCGIServer %1: remote socket still connected. errno: %2, x: %3" ) //
.arg( threadId )
.arg( errno )
.arg( x ),
5 );
}
else
int rv = select( mIpcFd + 1, &setOptions, NULL, NULL, &timeout ); // see https://stackoverflow.com/a/30395738
if ( rv == -1 )
{
// socket closed, nothing can be read
QgsDebugMsgLevel( QStringLiteral( "FCGIServer %1: remote socket has been closed! errno: %2, x: %3" ) //
QgsDebugMsgLevel( QStringLiteral( "FCGIServer %1: remote socket has been closed (select)! errno: %2" ) //
.arg( threadId )
.arg( errno )
.arg( x ),
.arg( errno ),
1 );
mFeedback->cancel();
break;
}
else
{
const ssize_t x = recv( mIpcFd, &c, 1, MSG_PEEK | MSG_DONTWAIT ); // see https://stackoverflow.com/a/12402596
if ( x != 0 )
{
// Ie. we are still connected but we have an 'error' as there is nothing to read
QgsDebugMsgLevel( QStringLiteral( "FCGIServer %1: remote socket still connected. errno: %2, x: %3" ) //
.arg( threadId )
.arg( errno )
.arg( x ),
5 );
}
else
{
// socket closed, nothing can be read
QgsDebugMsgLevel( QStringLiteral( "FCGIServer %1: remote socket has been closed (recv)! errno: %2, x: %3" ) //
.arg( threadId )
.arg( errno )
.arg( x ),
1 );
mFeedback->cancel();
break;
}
}

// If lock is acquired this means the response has finished and we will exit the while loop
// else we will wait max for 333ms.
Expand Down

0 comments on commit 55810cd

Please sign in to comment.