Ignore:
Timestamp:
Jun 8, 2016 2:55:24 AM (8 years ago)
Author:
ming
Message:

Close #1930: Race condition in OpenSSL socket

A workaround to solve the race condition based on ticket #985.

File:
1 edited

Legend:

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

    r5285 r5338  
    3838#define THIS_FILE               "ssl_sock_ossl.c" 
    3939 
    40 /* Workaround for ticket #985 */ 
    41 #define DELAYED_CLOSE_TIMEOUT   200 
     40/* Workaround for ticket #985 and #1930 */ 
     41#ifndef PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT 
     42#   define PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT    500 
     43#endif 
    4244 
    4345/*  
     
    808810 
    809811 
    810 /* Reset SSL socket state */ 
    811 static void reset_ssl_sock_state(pj_ssl_sock_t *ssock) 
    812 { 
    813     ssock->ssl_state = SSL_STATE_NULL; 
    814  
    815     destroy_ssl(ssock); 
    816  
     812/* Close sockets */ 
     813static void close_sockets(pj_ssl_sock_t *ssock) 
     814{ 
    817815    if (ssock->asock) { 
    818816        pj_activesock_close(ssock->asock); 
     
    824822        ssock->sock = PJ_INVALID_SOCKET; 
    825823    } 
     824} 
     825 
     826 
     827/* Reset SSL socket state */ 
     828static void reset_ssl_sock_state(pj_ssl_sock_t *ssock) 
     829{ 
     830    ssock->ssl_state = SSL_STATE_NULL; 
     831 
     832    destroy_ssl(ssock); 
     833 
     834    close_sockets(ssock); 
    826835 
    827836    /* Upon error, OpenSSL may leave any error description in the thread  
     
    11931202                      errmsg)); 
    11941203 
    1195             /* Workaround for ticket #985 */ 
    1196 #if (defined(PJ_WIN32) && PJ_WIN32!=0) || (defined(PJ_WIN64) && PJ_WIN64!=0) 
     1204            /* Originally, this is a workaround for ticket #985. However, 
     1205             * a race condition may occur in multiple worker threads 
     1206             * environment when we are destroying SSL objects while other 
     1207             * threads are still accessing them. 
     1208             * Please see ticket #1930 for more info. 
     1209             */ 
     1210#if 1 //(defined(PJ_WIN32) && PJ_WIN32!=0)||(defined(PJ_WIN64) && PJ_WIN64!=0) 
    11971211            if (ssock->param.timer_heap) { 
    1198                 pj_time_val interval = {0, DELAYED_CLOSE_TIMEOUT}; 
    1199  
    1200                 reset_ssl_sock_state(ssock); 
    1201  
     1212                pj_time_val interval = {0, PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT}; 
     1213 
     1214                ssock->ssl_state = SSL_STATE_NULL; 
     1215                close_sockets(ssock); 
     1216 
     1217                if (ssock->timer.id != TIMER_NONE) { 
     1218                    pj_timer_heap_cancel(ssock->param.timer_heap, 
     1219                                         &ssock->timer); 
     1220                } 
    12021221                ssock->timer.id = TIMER_CLOSE; 
    12031222                pj_time_val_normalize(&interval); 
     
    12051224                                           &ssock->timer, &interval) != 0) 
    12061225                { 
     1226                    PJ_LOG(3,(ssock->pool->obj_name, "Failed to schedule " 
     1227                              "a delayed close. Race condition may occur.")); 
    12071228                    ssock->timer.id = TIMER_NONE; 
    12081229                    pj_ssl_sock_close(ssock); 
    12091230                } 
    1210             } else  
    1211 #endif  /* PJ_WIN32 */ 
     1231            }  
     1232#else 
    12121233            { 
    12131234                pj_ssl_sock_close(ssock); 
    12141235            } 
     1236#endif 
     1237 
    12151238            return PJ_FALSE; 
    12161239        } 
     
    22162239    pj_ssl_sock_param_copy(pool, &ssock->param, param); 
    22172240    ssock->param.read_buffer_size = ((ssock->param.read_buffer_size+7)>>3)<<3; 
     2241    if (!ssock->param.timer_heap) { 
     2242        PJ_LOG(3,(ssock->pool->obj_name, "Warning: timer heap is not " 
     2243                  "available. It is recommended to supply one to avoid " 
     2244                  "a race condition if more than one worker threads " 
     2245                  "are used.")); 
     2246    } 
    22182247 
    22192248    /* Finally */ 
Note: See TracChangeset for help on using the changeset viewer.