[C][Client]Fix coredump in multi-thread environment (#7835)

This commit is contained in:
Hui Yu 2020-10-30 10:57:14 +08:00 committed by GitHub
parent 70323adf9f
commit 8f06876a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 6 deletions

View File

@ -7,7 +7,6 @@
size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);
apiClient_t *apiClient_create() {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
apiClient->basePath = strdup("{{{basePath}}}");
apiClient->sslConfig = NULL;
@ -43,7 +42,6 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
{{/authMethods}}
{{/hasAuthMethods}}
) {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
if(basePath){
apiClient->basePath = strdup(basePath);
@ -128,7 +126,6 @@ void apiClient_free(apiClient_t *apiClient) {
{{/authMethods}}
{{/hasAuthMethods}}
free(apiClient);
curl_global_cleanup();
}
sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) {
@ -620,3 +617,10 @@ char *strReplace(char *orig, char *rep, char *with) {
return result;
}
void apiClient_setupGlobalEnv() {
curl_global_init(CURL_GLOBAL_ALL);
}
void apiClient_unsetupGlobalEnv() {
curl_global_cleanup();
}

View File

@ -64,4 +64,16 @@ void sslConfig_free(sslConfig_t *sslConfig);
char *strReplace(char *orig, char *rep, char *with);
/*
* In single thread program, the function apiClient_setupGlobalEnv is not needed.
* But in multi-thread program, apiClient_setupGlobalEnv must be called before any worker thread is created
*/
void apiClient_setupGlobalEnv();
/*
* This function apiClient_unsetupGlobalEnv must be called whether single or multiple program.
* In multi-thread program, it is must be called after all worker threads end.
*/
void apiClient_unsetupGlobalEnv();
#endif // INCLUDE_API_CLIENT_H

View File

@ -46,4 +46,16 @@ void sslConfig_free(sslConfig_t *sslConfig);
char *strReplace(char *orig, char *rep, char *with);
/*
* In single thread program, the function apiClient_setupGlobalEnv is not needed.
* But in multi-thread program, apiClient_setupGlobalEnv must be called before any worker thread is created
*/
void apiClient_setupGlobalEnv();
/*
* This function apiClient_unsetupGlobalEnv must be called whether single or multiple program.
* In multi-thread program, it is must be called after all worker threads end.
*/
void apiClient_unsetupGlobalEnv();
#endif // INCLUDE_API_CLIENT_H

View File

@ -7,7 +7,6 @@
size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);
apiClient_t *apiClient_create() {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
apiClient->sslConfig = NULL;
@ -25,7 +24,6 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
, sslConfig_t *sslConfig
, list_t *apiKeys_api_key
) {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
if(basePath){
apiClient->basePath = strdup(basePath);
@ -82,7 +80,6 @@ void apiClient_free(apiClient_t *apiClient) {
free(apiClient->accessToken);
}
free(apiClient);
curl_global_cleanup();
}
sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) {
@ -526,3 +523,10 @@ char *strReplace(char *orig, char *rep, char *with) {
return result;
}
void apiClient_setupGlobalEnv() {
curl_global_init(CURL_GLOBAL_ALL);
}
void apiClient_unsetupGlobalEnv() {
curl_global_cleanup();
}

View File

@ -139,4 +139,6 @@ int main() {
fclose(file);
}
apiClient_free(apiClient3);
apiClient_unsetupGlobalEnv();
}

View File

@ -100,4 +100,6 @@ int main() {
}
list_free(elementToReturn);
apiClient_free(apiClient5);
apiClient_unsetupGlobalEnv();
}

View File

@ -120,4 +120,6 @@ int main() {
UserAPI_deleteUser(apiClient5, "example123");
apiClient_free(apiClient5);
apiClient_unsetupGlobalEnv();
}