Skip to content

Commit 0d0fe05

Browse files
committed
Move initialization of context to thread local object
The context global variable was already thread local, but its initialization was only being triggered during the initial import of the package. Moved all of the setup of the context variable to a __init__ function of a class that inherits from threading.local. This ensures that when a thread starts it will run the __init__ function. This avoids errors like the following: Traceback (most recent call last): File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run File "/root/src/es-cp4d-locust/env/lib/python3.6/site-packages/locust/user/users.py", line 161, in run_user user.run() File "/root/src/es-cp4d-locust/env/lib/python3.6/site-packages/locust/user/users.py", line 128, in run self.on_start() File "/root/src/es-cp4d-locust/locustfile.py", line 58, in on_start CFG['oc']['namespace']) File "/root/src/py-es-cp4d/db2_eventstore_cp4d/context.py", line 23, in __init__ oc.login(username, password) File "/root/src/openshift-client-python/packages/openshift/base_verbs.py", line 156, in login r.add_action(oc_action(cur_context(), "login", cmd_args=['-u', username, '-p', password, cmd_args])) File "/root/src/openshift-client-python/packages/openshift/context.py", line 37, in cur_context return context.stack[-1] File "src/gevent/local.py", line 408, in gevent._gevent_clocal.local.__getattribute__ AttributeError: 'gevent._gevent_clocal.local' object has no attribute 'stack'
1 parent 736bcf2 commit 0d0fe05

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

packages/openshift/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .ansible import ansible
1313

1414
# Single source for module version
15-
__VERSION__ = '1.0.3'
15+
__VERSION__ = '1.0.4'
1616

1717
null = None # Allow scripts to specify null in object definitions
1818

packages/openshift/context.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@
99

1010
from .result import Result
1111

12-
# All threads will have a context which is
13-
# managed by a stack of Context objects. As
14-
# a thread establish additional context using
15-
# 'with' statements, the stack will push/grow. As
16-
# 'with' blocks end, the stack will pop/shrink.
17-
context = local()
18-
19-
context.stack = []
20-
context.default_oc_path = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_OC_PATH", "oc") # Assume oc is in $PATH by default
21-
context.default_kubeconfig_path = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_CONFIG_PATH", None)
22-
context.default_api_server = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_API_SERVER", None)
23-
context.default_token = None # Does not support environment variable injection to discourage this insecure practice
24-
context.default_ca_cert_path = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_CA_CERT_PATH", None)
25-
context.default_project = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_PROJECT", None)
26-
context.default_options = {}
27-
context.default_loglevel = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_OC_LOGLEVEL", None)
28-
context.default_skip_tls_verify = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_SKIP_TLS_VERIFY", None)
29-
3012
# Provides defaults for ssh_client context instantiations
3113
DEFAULT_SSH_HOSTNAME = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_SSH_HOSTNAME", None)
3214
DEFAULT_SSH_USERNAME = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_SSH_USERNAME", None)
@@ -605,9 +587,29 @@ def timeout(seconds):
605587
return c
606588

607589

608-
root_context = Context()
609-
root_context.set_timeout(MASTER_TIMEOUT)
590+
class ThreadLocalContext(local):
591+
def __init__(self):
592+
self.default_oc_path = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_OC_PATH", "oc") # Assume oc is in $PATH by default
593+
self.default_kubeconfig_path = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_CONFIG_PATH", None)
594+
self.default_api_server = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_API_SERVER", None)
595+
self.default_token = None # Does not support environment variable injection to discourage this insecure practice
596+
self.default_ca_cert_path = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_CA_CERT_PATH", None)
597+
self.default_project = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_PROJECT", None)
598+
self.default_options = {}
599+
self.default_loglevel = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_OC_LOGLEVEL", None)
600+
self.default_skip_tls_verify = os.getenv("OPENSHIFT_CLIENT_PYTHON_DEFAULT_SKIP_TLS_VERIFY", None)
601+
602+
root_context = Context()
603+
root_context.set_timeout(MASTER_TIMEOUT)
610604

611-
# Ensure stack always has at least one member to simplify getting last
612-
# with [-1]
613-
context.stack = [root_context]
605+
# Ensure stack always has at least one member to simplify getting last
606+
# with [-1]
607+
self.stack = [root_context]
608+
609+
610+
# All threads will have a context which is
611+
# managed by a stack of Context objects. As
612+
# a thread establish additional context using
613+
# 'with' statements, the stack will push/grow. As
614+
# 'with' blocks end, the stack will pop/shrink.
615+
context = ThreadLocalContext()

0 commit comments

Comments
 (0)