Skip to content

Commit

Permalink
Merge pull request #31 from ksauzz/fix/race-conditions
Browse files Browse the repository at this point in the history
Fix race conditions
  • Loading branch information
ksauzz authored Apr 15, 2020
2 parents c2b9263 + f1b1107 commit 8881e65
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
15 changes: 13 additions & 2 deletions krbticket/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import logging
import os
import subprocess
import threading
from retrying import retry

logger = logging.getLogger(__name__)
# fasteners is for interprocess multiprocessing
# threading.Lock is for multithreading
lock = threading.Lock()


class KrbCommand():
Expand Down Expand Up @@ -59,6 +63,12 @@ def kdestroy(config):
commands.append(config.ccache_name)
return KrbCommand._call(config, commands)

@staticmethod
def cache_exists(config):
with lock:
with fasteners.InterProcessLock(config.ccache_cmd_lockfile):
return os.path.isfile(config.ccache_name)

@staticmethod
def _call(config, commands):

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

with fasteners.InterProcessLock(config.ccache_cmd_lockfile):
return retriable_call()
with lock:
with fasteners.InterProcessLock(config.ccache_cmd_lockfile):
return retriable_call()
16 changes: 14 additions & 2 deletions krbticket/ticket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from datetime import datetime
import logging
import threading
Expand Down Expand Up @@ -100,7 +99,7 @@ def get_instance(**kwargs):

@staticmethod
def cache_exists(config):
return os.path.isfile(config.ccache_name)
return KrbCommand.cache_exists(config)

@staticmethod
def init(principal, keytab=None, **kwargs):
Expand Down Expand Up @@ -158,3 +157,16 @@ def parseDatetime(str):
expires=parseDatetime(expires),
service_principal=service_principal,
renew_expires=parseDatetime(renew_expires))

@staticmethod
def _destroy():
"""
destroy internal ticket registry
stop all updaters belonging to a ticket registered in registry, and remove all entiries
"""
with KrbTicket.__instances_lock__:
for (key, ticket) in KrbTicket.__instances__.items():
ticket.updater().stop()
KrbCommand.kdestroy(ticket.config)
KrbTicket.__instances__ = {}
3 changes: 3 additions & 0 deletions tests/test_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import os
import subprocess

def teardown_function(function):
KrbTicket._destroy()


def test_init(config):
KrbCommand.kdestroy(config)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def teardown_function(function):
KrbTicket.__instances__ = {}
KrbTicket._destroy()


def test_updater(config):
Expand Down

0 comments on commit 8881e65

Please sign in to comment.