Skip to content

Commit be057bb

Browse files
authored
Fixes #15: Adds retry to shortcut creation to handle file contention (#22)
1 parent 9817099 commit be057bb

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/_native/shortcut.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,23 @@ shortcut_create(PyObject *, PyObject *args, PyObject *kwargs)
5555
err_SetFromWindowsErrWithMessage(hr, "Setting shortcut icon");
5656
goto done;
5757
}
58-
if (FAILED(hr = lnk->QueryInterface(&persist)) ||
59-
FAILED(hr = persist->Save(path, 0))) {
60-
err_SetFromWindowsErrWithMessage(hr, "Writing shortcut");
58+
if (FAILED(hr = lnk->QueryInterface(&persist))) {
59+
err_SetFromWindowsErrWithMessage(hr, "Getting persist interface");
6160
goto done;
6261
}
62+
// gh-15: Apparently ERROR_USER_MAPPED_FILE can occur here, which suggests
63+
// contention on the file. We should be able to sleep and retry to handle it
64+
// in most cases.
65+
for (int retries = 5; retries; --retries) {
66+
if (SUCCEEDED(hr = persist->Save(path, 0))) {
67+
break;
68+
}
69+
if (retries == 1 || hr != HRESULT_FROM_WIN32(ERROR_USER_MAPPED_FILE)) {
70+
err_SetFromWindowsErrWithMessage(hr, "Writing shortcut");
71+
goto done;
72+
}
73+
Sleep(10);
74+
}
6375

6476
r = Py_NewRef(Py_None);
6577

0 commit comments

Comments
 (0)