python_utils/start_port_server.py
Go to the documentation of this file.
1 # Copyright 2015 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from __future__ import print_function
16 
17 import logging
18 import os
19 import socket
20 import subprocess
21 import sys
22 import tempfile
23 import time
24 
25 import six.moves.urllib.request as request
26 
27 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
28 import jobset
29 
30 # must be synchronized with test/core/util/port_server_client.h
31 _PORT_SERVER_PORT = 32766
32 
33 
35  # check if a compatible port server is running
36  # if incompatible (version mismatch) ==> start a new one
37  # if not running ==> start a new one
38  # otherwise, leave it up
39  try:
40  version = int(
41  request.urlopen('http://localhost:%d/version_number' %
42  _PORT_SERVER_PORT).read())
43  logging.info('detected port server running version %d', version)
44  running = True
45  except Exception as e:
46  logging.exception('failed to detect port server')
47  running = False
48  if running:
49  current_version = int(
50  subprocess.check_output([
51  sys.executable, # use the same python binary as this process
52  os.path.abspath('tools/run_tests/python_utils/port_server.py'),
53  'dump_version'
54  ]).decode())
55  logging.info('my port server is version %d', current_version)
56  running = (version >= current_version)
57  if not running:
58  logging.info('port_server version mismatch: killing the old one')
59  request.urlopen('http://localhost:%d/quitquitquit' %
60  _PORT_SERVER_PORT).read()
61  time.sleep(1)
62  if not running:
63  fd, logfile = tempfile.mkstemp()
64  os.close(fd)
65  logging.info('starting port_server, with log file %s', logfile)
66  args = [
67  sys.executable,
68  os.path.abspath('tools/run_tests/python_utils/port_server.py'),
69  '-p',
70  '%d' % _PORT_SERVER_PORT, '-l', logfile
71  ]
72  env = dict(os.environ)
73  env['BUILD_ID'] = 'pleaseDontKillMeJenkins'
74  if jobset.platform_string() == 'windows':
75  # Working directory of port server needs to be outside of Jenkins
76  # workspace to prevent file lock issues.
77  tempdir = tempfile.mkdtemp()
78  if sys.version_info.major == 2:
79  creationflags = 0x00000008 # detached process
80  else:
81  creationflags = 0 # DETACHED_PROCESS doesn't seem to work with python3
82  port_server = subprocess.Popen(args,
83  env=env,
84  cwd=tempdir,
85  creationflags=creationflags,
86  close_fds=True)
87  else:
88  port_server = subprocess.Popen(args,
89  env=env,
90  preexec_fn=os.setsid,
91  close_fds=True)
92  time.sleep(1)
93  # ensure port server is up
94  waits = 0
95  while True:
96  if waits > 10:
97  logging.warning(
98  'killing port server due to excessive start up waits')
99  port_server.kill()
100  if port_server.poll() is not None:
101  logging.error('port_server failed to start')
102  # try one final time: maybe another build managed to start one
103  time.sleep(1)
104  try:
105  request.urlopen('http://localhost:%d/get' %
106  _PORT_SERVER_PORT).read()
107  logging.info(
108  'last ditch attempt to contact port server succeeded')
109  break
110  except:
111  logging.exception(
112  'final attempt to contact port server failed')
113  port_log = open(logfile, 'r').read()
114  print(port_log)
115  sys.exit(1)
116  try:
117  port_server_url = 'http://localhost:%d/get' % _PORT_SERVER_PORT
118  request.urlopen(port_server_url).read()
119  logging.info('port server is up and ready')
120  break
121  except socket.timeout:
122  logging.exception('while waiting for port_server')
123  time.sleep(1)
124  waits += 1
125  except IOError:
126  logging.exception('while waiting for port_server')
127  time.sleep(1)
128  waits += 1
129  except:
130  logging.exception(
131  'error while contacting port server at "%s".'
132  'Will try killing it.', port_server_url)
133  port_server.kill()
134  raise
python_utils.start_port_server.start_port_server
def start_port_server()
Definition: python_utils/start_port_server.py:34
xds_interop_client.int
int
Definition: xds_interop_client.py:113
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
grpc._common.decode
def decode(b)
Definition: grpc/_common.py:75
open
#define open
Definition: test-fs.c:46


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:22