From f8ca35f9b6b940be868798f84b4ca082a5503f82 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 8 Sep 2010 20:16:57 -0700 Subject: [PATCH] call accept() in main thread rather than in thread proc to fix long-standing bug where main thread kept finding socket to be readable and forking new threads to call accept() on it and then block forever. --- xwords4/relay/http.cpp | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/xwords4/relay/http.cpp b/xwords4/relay/http.cpp index 827ea98aa..8c20da65c 100644 --- a/xwords4/relay/http.cpp +++ b/xwords4/relay/http.cpp @@ -197,14 +197,22 @@ printStats( FILE* fil, const CrefMgrInfo* info, bool isLocal ) fprintf( fil, "" ); } +class HttpInstance { + public: + HttpInstance( int sock, HttpState* state ) { + m_sock = sock; + m_state = state; + } + int m_sock; + HttpState* m_state; +}; + static void* http_thread_main( void* arg ) { - HttpState* state = (HttpState*)arg; - - sockaddr newaddr; - socklen_t siz = sizeof(newaddr); - int sock = accept( state->ctrl_sock, &newaddr, &siz ); + HttpInstance* inst = (HttpInstance*)arg; + HttpState* state = inst->m_state; + int sock = inst->m_sock; char buf[512]; ssize_t totalRead = 0; @@ -275,6 +283,8 @@ http_thread_main( void* arg ) } close( sock ); + delete inst; + return NULL; } /* http_thread_main */ @@ -282,13 +292,18 @@ void run_http_thread( HttpState* state ) { pthread_t thread; - int result = pthread_create( &thread, NULL, - http_thread_main, (void*)state ); + sockaddr newaddr; + socklen_t siz = sizeof(newaddr); + int sock = accept( state->ctrl_sock, &newaddr, &siz ); + + HttpInstance* inst = new HttpInstance( sock, state ); + int result = pthread_create( &thread, NULL, http_thread_main, + (void*)inst ); if ( 0 == result ) { pthread_detach( thread ); } else { - /* logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__, */ - /* strerror(errno) ); */ + logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__, + strerror(errno) ); } } /* run_http_thread */