Changeset 5343 for pjproject


Ignore:
Timestamp:
Jun 14, 2016 10:28:19 AM (8 years ago)
Author:
ming
Message:

Re #1930: Fixed race condition when more than one thread try to close the same activesock.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/ssl_sock_ossl.c

    r5338 r5343  
    813813static void close_sockets(pj_ssl_sock_t *ssock) 
    814814{ 
    815     if (ssock->asock) { 
    816         pj_activesock_close(ssock->asock); 
    817         ssock->asock = NULL; 
    818         ssock->sock = PJ_INVALID_SOCKET; 
    819     } 
    820     if (ssock->sock != PJ_INVALID_SOCKET) { 
    821         pj_sock_close(ssock->sock); 
    822         ssock->sock = PJ_INVALID_SOCKET; 
    823     } 
     815    pj_activesock_t *asock; 
     816    pj_sock_t sock; 
     817 
     818    /* This can happen when pj_ssl_sock_create() fails. */ 
     819    if (!ssock->write_mutex) 
     820        return; 
     821 
     822    pj_lock_acquire(ssock->write_mutex); 
     823    asock = ssock->asock; 
     824    if (asock) { 
     825        ssock->asock = NULL; 
     826        ssock->sock = PJ_INVALID_SOCKET; 
     827    } 
     828    sock = ssock->sock; 
     829    if (sock != PJ_INVALID_SOCKET) 
     830        ssock->sock = PJ_INVALID_SOCKET; 
     831    pj_lock_release(ssock->write_mutex); 
     832 
     833    if (asock) 
     834        pj_activesock_close(asock); 
     835 
     836    if (sock != PJ_INVALID_SOCKET) 
     837        pj_sock_close(sock); 
    824838} 
    825839 
     
    19461960                                        &ssock->timer, 
    19471961                                        &ssock->param.timeout); 
    1948         if (status != PJ_SUCCESS) 
     1962        if (status != PJ_SUCCESS) { 
    19491963            ssock->timer.id = TIMER_NONE; 
     1964            status = PJ_SUCCESS; 
     1965        } 
    19501966    } 
    19511967 
     
    19531969    ssock->ssl_state = SSL_STATE_HANDSHAKING; 
    19541970    SSL_set_accept_state(ssock->ossl_ssl); 
    1955     status = do_handshake(ssock); 
     1971    //To avoid race condition, we don't need to do it here and 
     1972    //let the handshake happen in ssock->asock's callback instead. 
     1973    //status = do_handshake(ssock); 
    19561974 
    19571975on_return: 
    1958     if (ssock && status != PJ_EPENDING) 
    1959         on_handshake_complete(ssock, status); 
     1976    if (ssock && status != PJ_SUCCESS) { 
     1977        //on_handshake_complete(ssock, status); 
     1978        close_sockets(ssock); 
     1979    } 
    19601980 
    19611981    /* Must return PJ_TRUE whatever happened, as active socket must  
     
    28642884                                        &ssock->timer, 
    28652885                                        &ssock->param.timeout); 
    2866         if (status != PJ_SUCCESS) 
     2886        if (status != PJ_SUCCESS) { 
    28672887            ssock->timer.id = TIMER_NONE; 
     2888            status = PJ_SUCCESS; 
     2889        } 
    28682890    } 
    28692891 
Note: See TracChangeset for help on using the changeset viewer.