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.
This commit is contained in:
Eric House 2010-09-08 20:16:57 -07:00
parent e20c63f38c
commit 90d505abea

View file

@ -197,14 +197,22 @@ printStats( FILE* fil, const CrefMgrInfo* info, bool isLocal )
fprintf( fil, "</table>" ); fprintf( fil, "</table>" );
} }
class HttpInstance {
public:
HttpInstance( int sock, HttpState* state ) {
m_sock = sock;
m_state = state;
}
int m_sock;
HttpState* m_state;
};
static void* static void*
http_thread_main( void* arg ) http_thread_main( void* arg )
{ {
HttpState* state = (HttpState*)arg; HttpInstance* inst = (HttpInstance*)arg;
HttpState* state = inst->m_state;
sockaddr newaddr; int sock = inst->m_sock;
socklen_t siz = sizeof(newaddr);
int sock = accept( state->ctrl_sock, &newaddr, &siz );
char buf[512]; char buf[512];
ssize_t totalRead = 0; ssize_t totalRead = 0;
@ -275,6 +283,8 @@ http_thread_main( void* arg )
} }
close( sock ); close( sock );
delete inst;
return NULL; return NULL;
} /* http_thread_main */ } /* http_thread_main */
@ -282,13 +292,18 @@ void
run_http_thread( HttpState* state ) run_http_thread( HttpState* state )
{ {
pthread_t thread; pthread_t thread;
int result = pthread_create( &thread, NULL, sockaddr newaddr;
http_thread_main, (void*)state ); 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 ) { if ( 0 == result ) {
pthread_detach( thread ); pthread_detach( thread );
} else { } else {
/* logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__, */ logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__,
/* strerror(errno) ); */ strerror(errno) );
} }
} /* run_http_thread */ } /* run_http_thread */