[New-bugs-announce] [issue36656] Allow os.symlink(src, target, force=True) to prevent race conditions

Tom Hale report at bugs.python.org
Thu Apr 18 04:44:47 EDT 2019


New submission from Tom Hale <tomnott at gmail.com>:

I cannot find a race-condition-free way to force overwrite an existing symlink.

os.symlink() requires that the target does not exist, meaning that it could be created via race condition the two workaround solutions that I've seen:

1. Unlink existing symlink (could be recreated, causing following symlink to fail)

2. Create a new temporary symlink, then overwrite target (temp could be changed between creation and replace.

The additional gotcha with the safer (because the attack filename is unknown) option (2) is that replace() may fail if the two files are on separate filesystems.

I suggest an additional `force=` argument to os.symlink(), defaulting to `False` for backward compatibility, but allowing atomic overwriting of a symlink when set to `True`.

I would be willing to look into a PR for this.

Prior art:  https://stackoverflow.com/a/55742015/5353461

----------
messages: 340474
nosy: Tom Hale
priority: normal
severity: normal
status: open
title: Allow os.symlink(src, target, force=True) to prevent race conditions
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36656>
_______________________________________


More information about the New-bugs-announce mailing list