0

I am using threading to post data on server.threading is working fine and Data is receiving at the server but Every time i am getting Segmentation Fault/core dump exception for every request.

My thread function

void* APR_THREAD_FUNC thread_init(apr_thread_t *thd,void *data)
{
DATA_INFO_SOCK *dt=(DATA_INFO_SOCK*)data;
apr_status_t result;
result=apr_socket_sendto(dt->new_sock,dt->addr,0,dt->payload,dt->len);
if(result==APR_SUCCESS)
{
 result=apr_socket_recvfrom(dt->addr,dt->new_sock,0,dt->resp,dt->rec_len);
}
result=apr_socket_close(dt->new_sock);
apr_thread_exit(thd, APR_SUCCESS);
}

My thread initiating C code

int post_data_async(request_rec *r, char *host,char *encoded_data,int timout)
{
apr_status_t result;
apr_thread_t *thrd;
apr_threadattr_t *thrdattr;
char *final_msg;
apr_socket_t *new_sock;
apr_sockaddr_t *addr;
DATA_INFO_SOCK *dt=apr_palloc(r->pool,sizeof(DATA_INFO_SOCK));
char *hostname=host;
char *msg = "POST /getRequestData HTTP/1.1\r\n"\
            "HOST: %s\r\n" \
            "Accept: */*\r\n"\
            "Connection: keep-alive\r\n"\
            "Content-Length: %d\r\n"\
            "Content-Type: application/x-www-form-urlencoded\r\n\n%s";
apr_port_t port_no=80;
apr_interval_time_t tmout=timout;
apr_size_t *size=apr_palloc(r->pool,sizeof(apr_size_t));
apr_size_t *rcv=apr_palloc(r->pool,sizeof(apr_size_t));
final_msg=apr_psprintf(r->pool,msg,host,strlen(encoded_data),encoded_data);
result=apr_socket_create(&dt->new_sock,APR_INET,SOCK_STREAM,APR_PROTO_TCP,r->pool);
result=apr_sockaddr_info_get(&dt->addr,hostname,APR_INET,port_no,0,r->pool);
result=apr_socket_connect(dt->new_sock,dt->addr);
//result=apr_socket_timeout_set(dt->new_sock,tmout);
*size=strlen(final_msg);
//data preparation for thread
dt->r=r;
dt->resp=apr_palloc(r->pool,sizeof(char)*1024);
dt->payload=final_msg;
dt->len=size;
dt->rec_len=rcv;
result=apr_thread_create(&thrd,NULL,thread_init,(void*)dt,r->pool);
ap_rputs("resule after thread",r);
return 0;
}

1 Answers1

0

You can't use r or r->pool if your async request completion does not block the completion of the current request. Otherwise your memory is likely to be reused, causing segfaults.

(If it did block request completion, there's probably little reason to do it async)

You should create a (non r->pool subpool) pool for your async work item if you need to use APR code in it.

covener
  • 17,402
  • 2
  • 31
  • 45