Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 8881e65

Browse files
authored
Merge pull request #31 from ksauzz/fix/race-conditions
Fix race conditions
2 parents c2b9263 + f1b1107 commit 8881e65

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

krbticket/command.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
import logging
33
import os
44
import subprocess
5+
import threading
56
from retrying import retry
67

78
logger = logging.getLogger(__name__)
9+
# fasteners is for interprocess multiprocessing
10+
# threading.Lock is for multithreading
11+
lock = threading.Lock()
812

913

1014
class KrbCommand():
@@ -59,6 +63,12 @@ def kdestroy(config):
5963
commands.append(config.ccache_name)
6064
return KrbCommand._call(config, commands)
6165

66+
@staticmethod
67+
def cache_exists(config):
68+
with lock:
69+
with fasteners.InterProcessLock(config.ccache_cmd_lockfile):
70+
return os.path.isfile(config.ccache_name)
71+
6272
@staticmethod
6373
def _call(config, commands):
6474

@@ -78,5 +88,6 @@ def retriable_call():
7888
custom_env["LC_ALL"] = "C"
7989
return subprocess.check_output(commands, universal_newlines=True, env=custom_env)
8090

81-
with fasteners.InterProcessLock(config.ccache_cmd_lockfile):
82-
return retriable_call()
91+
with lock:
92+
with fasteners.InterProcessLock(config.ccache_cmd_lockfile):
93+
return retriable_call()

krbticket/ticket.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from datetime import datetime
32
import logging
43
import threading
@@ -100,7 +99,7 @@ def get_instance(**kwargs):
10099

101100
@staticmethod
102101
def cache_exists(config):
103-
return os.path.isfile(config.ccache_name)
102+
return KrbCommand.cache_exists(config)
104103

105104
@staticmethod
106105
def init(principal, keytab=None, **kwargs):
@@ -158,3 +157,16 @@ def parseDatetime(str):
158157
expires=parseDatetime(expires),
159158
service_principal=service_principal,
160159
renew_expires=parseDatetime(renew_expires))
160+
161+
@staticmethod
162+
def _destroy():
163+
"""
164+
destroy internal ticket registry
165+
166+
stop all updaters belonging to a ticket registered in registry, and remove all entiries
167+
"""
168+
with KrbTicket.__instances_lock__:
169+
for (key, ticket) in KrbTicket.__instances__.items():
170+
ticket.updater().stop()
171+
KrbCommand.kdestroy(ticket.config)
172+
KrbTicket.__instances__ = {}

tests/test_ticket.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import os
55
import subprocess
66

7+
def teardown_function(function):
8+
KrbTicket._destroy()
9+
710

811
def test_init(config):
912
KrbCommand.kdestroy(config)

tests/test_updater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def teardown_function(function):
11-
KrbTicket.__instances__ = {}
11+
KrbTicket._destroy()
1212

1313

1414
def test_updater(config):

0 commit comments

Comments
 (0)