From 4aee7a6c618e646161043e82f67e9c28533c4f2a Mon Sep 17 00:00:00 2001 From: shortcutme Date: Wed, 20 Mar 2019 01:05:52 +0100 Subject: [PATCH] Make openLocked always return BlockingIOError on fail --- src/Test/TestHelper.py | 2 +- src/main.py | 2 +- src/util/helper.py | 23 +++++++++++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Test/TestHelper.py b/src/Test/TestHelper.py index 14aa2233..27465cba 100644 --- a/src/Test/TestHelper.py +++ b/src/Test/TestHelper.py @@ -68,7 +68,7 @@ class TestHelper: def testOpenLocked(self): locked_f = helper.openLocked(config.data_dir + "/locked.file") assert locked_f - with pytest.raises(PermissionError): + with pytest.raises(BlockingIOError): locked_f_again = helper.openLocked(config.data_dir + "/locked.file") locked_f_different = helper.openLocked(config.data_dir + "/locked_different.file") diff --git a/src/main.py b/src/main.py index 818ed8a2..be25c0fd 100644 --- a/src/main.py +++ b/src/main.py @@ -39,7 +39,7 @@ if config.action == "main": try: lock = helper.openLocked("%s/lock.pid" % config.data_dir, "w") lock.write("%s" % os.getpid()) - except IOError as err: + except BlockingIOError as err: print("Can't open lock file, your ZeroNet client is probably already running, exiting... (%s)" % err) if config.open_browser and config.open_browser != "False": print("Opening browser: %s...", config.open_browser) diff --git a/src/util/helper.py b/src/util/helper.py index ffa3afc7..547fcd40 100644 --- a/src/util/helper.py +++ b/src/util/helper.py @@ -37,16 +37,19 @@ def atomicWrite(dest, content, mode="wb"): def openLocked(path, mode="wb"): - if os.name == "posix": - import fcntl - f = open(path, mode) - fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) - elif os.name == "nt": - import msvcrt - f = open(path, mode) - msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1) - else: - f = open(path, mode) + try: + if os.name == "posix": + import fcntl + f = open(path, mode) + fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) + elif os.name == "nt": + import msvcrt + f = open(path, mode) + msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1) + else: + f = open(path, mode) + except (IOError, PermissionError, BlockingIOError) as err: + raise BlockingIOError("Unable to lock file: %s" % err) return f