Skip to content

Commit 06b2dfe

Browse files
Better handling in cache source path + fix thread leak
1 parent fda6002 commit 06b2dfe

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

graphics.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6262
if (uMsg == ioMsg) {
6363
req = (Request*)lParam;
6464
done = handle_io_progress(req);
65+
SetEvent(req->readyEvent);
6566
if (done) {
6667
free(req);
67-
} else {
68-
SetEvent(req->readyEvent);
6968
}
7069
return 0;
7170
}

requests.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ handle_io_progress(Request *req)
180180
CloseHandle(req->handles.hFile);
181181
}
182182

183+
if (req->source == REQ_SRC_CACHE) {
184+
UnlockUrlCacheEntryFileA(req->originalUrl, 0);
185+
}
186+
183187
if (req->source == REQ_SRC_HTTP) {
184188
if (req->handles.http.hReq != NULL) {
185189
InternetCloseHandle(req->handles.http.hReq);
@@ -248,7 +252,8 @@ try_init_from_cache(Request *req)
248252
}
249253

250254
/* If we have internet, check the last modified time of the resource. If it's newer than what we have cached, bail. */
251-
hUrl = InternetOpenUrlA(hInternet, req->url, NULL, 0, 0, (DWORD_PTR)NULL);
255+
hUrl = InternetOpenUrlA(hInternet, req->url, NULL, 0,
256+
INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD_PTR)NULL);
252257
lenlen = sizeof(modifiedTimeSys);
253258
if (hUrl != NULL
254259
&& HttpQueryInfoA(hUrl, HTTP_QUERY_FLAG_SYSTEMTIME | HTTP_QUERY_LAST_MODIFIED, &modifiedTimeSys, &lenlen, 0)
@@ -268,6 +273,9 @@ try_init_from_cache(Request *req)
268273
success = true;
269274

270275
cleanup:
276+
if (!success) {
277+
UnlockUrlCacheEntryFileA(req->url, 0);
278+
}
271279
if (hUrl != NULL) {
272280
InternetCloseHandle(hUrl);
273281
}
@@ -494,16 +502,24 @@ VOID CALLBACK
494502
handle_request(PTP_CALLBACK_INSTANCE inst, void *reqArg, PTP_WORK work)
495503
{
496504
Request *req;
505+
bool done;
497506

498507
req = (Request *)reqArg;
499-
while (!req->done) {
508+
done = false;
509+
while (!done) {
500510
if (req->source == REQ_SRC_UNSET) {
501511
init_request(req);
502512
}
503513
if (!req->failed) {
504514
progress_request(req);
505515
}
506516

517+
/*
518+
* Capture done state before posting to the main thread,
519+
* since the main thread may free req after processing.
520+
*/
521+
done = req->done || req->failed;
522+
507523
if (req->hOutFile != INVALID_HANDLE_VALUE) {
508524
/* write directly to output file */
509525
DWORD written;

0 commit comments

Comments
 (0)