kubernetes-client / kubernetes-client/python
flask app multithread using kubernetes-client: ApiExecption(0) Handshake status 200 OK
- Dominant language
- Python
- Stars
- 7.7k
- Forks
- 3.5k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 18
Description
Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "/home/h37/X_Server/ServerController/app/views/user_view.py", line 78, in user_center
navigations=view_util.get_navi_path(proj_name),
File "/home/h37/X_Server/ServerController/app/views/view_util.py", line 98, in get_navi_path
branches = KubeCtl().get_project_hold_server_branches(project_name)
File "/home/h37/X_Server/ServerController/app/kubectl/KubeCtl.py", line 381, in get_project_hold_server_branches
pod_lst = self.v1_api.list_namespaced_pod(project_name)
File "/usr/local/lib/python2.7/dist-packages/kubernetes/client/apis/core_v1_api.py", line 12372, in list_namespaced_pod
(data) = self.list_namespaced_pod_with_http_info(namespace, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/kubernetes/client/apis/core_v1_api.py", line 12472, in list_namespaced_pod_with_http_info
collection_formats=collection_formats)
File "/usr/local/lib/python2.7/dist-packages/kubernetes/client/api_client.py", line 334, in call_api
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
File "/usr/local/lib/python2.7/dist-packages/kubernetes/client/api_client.py", line 168, in __call_api
_request_timeout=_request_timeout)
File "/usr/local/lib/python2.7/dist-packages/kubernetes/stream/stream.py", line 31, in _intercept_request_call
return ws_client.websocket_call(config, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/kubernetes/stream/ws_client.py", line 260, in websocket_call
raise ApiException(status=0, reason=str(e))
ApiException: (0)
Reason: Handshake status 200 OK
In my flask application, using multithread mode.
And, if one request need execute a connect_post_namespaced_pod_exec function (I invoke it by using stream.). The connect_post_namespaced_pod_exec will cost minutes to finished. While it is not finished, some other normal request comming, for example, list_namespaced_pod.
It throws the trace shown above. I find the stream implement is replace the api_client.request by ws_client like:
`
def stream(func, *args, **kwargs):
"""Stream given API call using websocket"""
def _intercept_request_call(*args, **kwargs):
# old generated code's api client has config. new ones has
# configuration
try:
config = func.__self__.api_client.configuration
except AttributeError:
config = func.__self__.api_client.config
return ws_client.websocket_call(config, *args, **kwargs)
prev_request = func.__self__.api_client.request
try:
func.__self__.api_client.request = _intercept_request_call
return func(*args, **kwargs)
finally:
func.__self__.api_client.request = prev_request
`
So, when connect_post_namespaced_pod_exec not finished, later requests will throw trace.
My question:
It is kubernetes client does not support multi-thread scene?
Or,it there a method to solve this problem.
My bottom line plan is to execute commands from os.system('cmd'). It will limit my flask application must be deploy on cluster master node.
Contributor guide
Research direction
Start with kubernetes/stream/stream.py and kubernetes/stream/ws_client.py, then trace the calls through kubernetes/client/api_client.py and the Flask view paths shown in the traceback. Reproduce a long-running connect_post_namespaced_pod_exec alongside list_namespaced_pod in multithreaded mode and determine whether shared request interception causes the handshake error. Done means the concurrency behavior and a supported resolution are established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, kubernetes, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100