8000 Try to prevent a PermissionError on Windows · python/cpython@09adb2b · GitHub
[go: up one dir, main page]

Skip to content

Commit 09adb2b

Browse files
Try to prevent a PermissionError on Windows
If `NamedTemporaryFile.__exit__` tries to delete the connect script before the remote process has closed it, a `PermissionError` is raised on Windows. Unfortunately there is no synchronization that we can use to ensure that the file is closed before we try to delete it. This makes the race less likely to occur by making the `with` block contain more code, so that more time passes before we attempt to delete the file. Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
1 parent 5d59ce1 commit 09adb2b

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Lib/pdb.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,31 +3044,31 @@ def attach(pid):
30443044
with closing(socket.create_server(("localhost", 0))) as server:
30453045
port = server.getsockname()[1]
30463046

3047-
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as script:
3048-
script.write(
3047+
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as connect_script:
3048+
connect_script.write(
30493049
f'import pdb, sys\n'
30503050
f'pdb._connect("localhost", {port}, sys._getframe().f_back)\n'
30513051
)
3052-
script.close()
3053-
sys.remote_exec(pid, script.name)
3052+
connect_script.close()
3053+
sys.remote_exec(pid, connect_script.name)
30543054

30553055
# TODO Add a timeout? Or don't bother since the user can ^C?
30563056
client_sock, _ = server.accept()
30573057

3058-
with closing(client_sock):
3059-
sockfile = client_sock.makefile("rwb")
3058+
with closing(client_sock):
3059+
sockfile = client_sock.makefile("rwb")
30603060

3061-
with closing(sockfile):
3062-
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as script:
3063-
script.write(
3064-
'import pdb, sys\n'
3065-
'if inst := pdb.Pdb._last_pdb_instance:\n'
3066-
' inst.set_step()\n'
3067-
' inst.set_trace(sys._getframe(1))\n'
3068-
)
3069-
script.close()
3061+
with closing(sockfile):
3062+
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as interrupt_script:
3063+
interrupt_script.write(
3064+
'import pdb, sys\n'
3065+
'if inst := pdb.Pdb._last_pdb_instance:\n'
3066+
' inst.set_step()\n'
3067+
' inst.set_trace(sys._getframe(1))\n'
3068+
)
3069+
interrupt_script.close()
30703070

3071-
_PdbClient(pid, sockfile, script.name).cmdloop()
3071+
_PdbClient(pid, sockfile, interrupt_script.name).cmdloop()
30723072

30733073

30743074
# Post-Mortem interface

0 commit comments

Comments
 (0)
0