This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python crashes when calling win32file.LockFileEx
Type: crash Stage: resolved
Components: IDLE, Windows Versions: Python 3.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: eryksun, paul.moore, steve.dower, terry.reedy, tim.golden, yapydev, zach.ware
Priority: normal Keywords:

Created on 2019-04-26 21:57 by yapydev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg340947 - (view) Author: Anand Arumugam (yapydev) Date: 2019-04-26 21:57
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32file
>>> import win32con
>>> import platform
>>> h = win32file.CreateFile(f'd:\\temp\\{platform.node()}.lock', (win32file.GENERIC_READ | win32file.GENERIC_WRITE), 0, None, win32con.CREATE_NEW, 0, None)
>>> win32file.LockFileEx(h, win32con.LOCKFILE_EXCLUSIVE_LOCK, 5, 5, None)

The moment I hit enter, python command prompt crashes. I'm unable to attach the crash dump file. If you cannot repro the crash, let me know.
msg340948 - (view) Author: Anand Arumugam (yapydev) Date: 2019-04-26 22:00
Here's the crash call stack:

Debug session time: Fri Apr 26 14:39:04.000 2019 (UTC - 7:00)
System Uptime: 3 days 12:26:46.641
Process Uptime: 0 days 0:02:27.000
...............................................
This dump file has an exception of interest stored in it.
The stored exception information can be accessed via .ecxr.
(70a8.52a0): Access violation - code c0000005 (first/second chance not available)
For analysis of this file, run !analyze -v
ntdll!ZwWaitForMultipleObjects+0x14:
00007ffc`c67cf114 c3              ret
0:000> kc
 # Call Site
00 ntdll!ZwWaitForMultipleObjects
01 KERNELBASE!WaitForMultipleObjectsEx
02 KERNELBASE!WaitForMultipleObjects
03 kernel32!WerpReportFaultInternal
04 kernel32!WerpReportFault
05 KERNELBASE!UnhandledExceptionFilter
06 ntdll!RtlpThreadExceptionFilter
07 ntdll!RtlUserThreadStart$filt$0
08 ntdll!__C_specific_handler
09 ntdll!RtlpExecuteHandlerForException
0a ntdll!RtlDispatchException
0b ntdll!KiUserExceptionDispatch
*** WARNING: Unable to verify checksum for win32file.pyd
0c KERNELBASE!LockFileEx
0d win32file
0e python37!_PyMethodDef_RawFastCallKeywords
0f python37!_PyCFunction_FastCallKeywords
10 python37!call_function
11 python37!_PyEval_EvalFrameDefault
12 python37!PyEval_EvalFrameEx
13 python37!_PyEval_EvalCodeWithName
14 python37!PyEval_EvalCodeEx
15 python37!PyEval_EvalCode
16 python37!run_mod
17 python37!PyRun_InteractiveOneObjectEx
18 python37!PyRun_InteractiveLoopFlags
19 python37!PyRun_AnyFileExFlags
1a python37!pymain_run_file
1b python37!pymain_run_filename
1c python37!pymain_run_python
1d python37!pymain_main
1e python37!Py_Main
1f python!invoke_main
20 python!__scrt_common_main_seh
21 kernel32!BaseThreadInitThunk
22 ntdll!RtlUserThreadStart
msg340985 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-04-27 14:25
> win32file.LockFileEx(h, win32con.LOCKFILE_EXCLUSIVE_LOCK, 5, 5, None)

This is a third-party issue in the PyWin32 package. win32file.LockFileEx shouldn't allow passing None for the required 5th parameter `ol` (i.e. the lpOverlapped parameter of WINAPI LockFileEx [1]). Or at least it should allocate a default overlapped record in this case, with the offset set to 0. Passing a NULL pointer leads to an access-violation exception.

[1]: https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex

FYI, setting a lock on the file is unecessary in this case. You created the kernel file object without data-access sharing (i.e. shareMode == 0). Until the handle is closed, opening the file again with read or write data access will fail as a sharing violation (32).
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80917
2019-04-27 14:25:47eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg340985

resolution: third party
stage: resolved
2019-04-26 22:00:57yapydevsetmessages: + msg340948
2019-04-26 21:57:50yapydevcreate