Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible improper locking issues #7926

Closed
ryancaicse opened this issue Oct 30, 2021 · 4 comments
Closed

Possible improper locking issues #7926

ryancaicse opened this issue Oct 30, 2021 · 4 comments

Comments

@ryancaicse
Copy link

@ryancaicse ryancaicse commented Oct 30, 2021

HI, in this function, it is possible that the lock lock (Line 71) is not released properly because the execution does not enter the while(j < num_urls)? Also, it also seems that the lock lock could be released multiple times in the while loop at Line 97.

Thanks for checking.

void *pull_one_url(void *NaN)
{
/* Stop threads from entering unless j is incremented */
pthread_mutex_lock(&lock);
while(j < num_urls) {
CURL *curl;
gchar *http;
printf("j = %d\n", j);
http =
g_strdup_printf("xoap.weather.com/weather/local/%s?cc=*&dayf=5&unit=i\n",
urls[j]);
printf("http %s", http);
curl = curl_easy_init();
if(curl) {
FILE *outfile = fopen(urls[j], "wb");
/* Set the URL and transfer type */
curl_easy_setopt(curl, CURLOPT_URL, http);
/* Write to the file */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
j++; /* critical line */
pthread_mutex_unlock(&lock);
curl_easy_perform(curl);
fclose(outfile);
printf("fclose\n");
curl_easy_cleanup(curl);
}
g_free(http);
/* Adds more latency, testing the mutex.*/
sleep(1);
} /* end while */
return NULL;
}

@bagder
Copy link
Member

@bagder bagder commented Oct 30, 2021

It does indeed look very wrong. You up for writing a pull request and fix it?

Loading

@ryancaicse
Copy link
Author

@ryancaicse ryancaicse commented Oct 30, 2021

Sorry, I don't know which is the best way to fix them due to being unaware of the shared data protected by the lock. But, here is my try. Is this enough to fix this issue?

 void *pull_one_url(void *NaN) 
 { 
   /* Stop threads from entering unless j is incremented */ 
   pthread_mutex_lock(&lock); 
   while(j < num_urls) { 
     ...;
     if(curl) { 
      ...;
       pthread_mutex_unlock(&lock); 
  
       ...;
       pthread_mutex_lock(&lock); // new added
     } 
     ...;
   } /* end while */ 
    pthread_mutex_unlock(&lock); // new added
   return NULL; 
 }

Loading

bagder added a commit that referenced this issue Oct 31, 2021
Reported-by: ryancaicse on github
Fixes #7926
@bagder
Copy link
Member

@bagder bagder commented Oct 31, 2021

I think it needs more than that. See #7931 for my proposed fix.

Loading

@ryancaicse
Copy link
Author

@ryancaicse ryancaicse commented Nov 1, 2021

OK, thanks

Loading

@bagder bagder closed this in f907fae Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants