Skip to content

Commit

Permalink
buildmaster/ssh: Fix leaked ssh connections
Browse files Browse the repository at this point in the history
* In the event of a retry loop, we previously held ssh sessions open
  which resulted in an ever-increasing number of open ssh connections.
  • Loading branch information
kallisti5 committed Aug 30, 2024
1 parent d2e611f commit 3f6ba19
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions HaikuPorter/Builders/RemoteBuilderSSH.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def __init__(self, configFilePath, packageRepository, outputBaseDir,
self.portsTreeHead = portsTreeHead
self.packageRepository = packageRepository

self.sshClient = None
self.jumpClient = None

if not paramiko:
raise Exception('paramiko unavailable')

Expand Down Expand Up @@ -129,26 +132,24 @@ def _loadConfig(self, configFilePath):

def _connect(self):
try:
transport = None
if 'jumpHost' in self.config['ssh']:
jumphost=paramiko.SSHClient()
jumphost.load_host_keys(self.config['ssh']['hostKeyFile'])
self.jumpClient=paramiko.SSHClient()
self.jumpClient.load_host_keys(self.config['ssh']['hostKeyFile'])
self.logger.info('trying to connect to jumphost for builder ' + self.name)
jumphost.connect(self.config['ssh']['jumpHost'],
self.jumpClient.connect(self.config['ssh']['jumpHost'],
port=int(self.config['ssh']['jumpPort']),
username=self.config['ssh']['jumpUser'],
key_filename=self.config['ssh']['jumpPrivateKeyFile'],
compress=True, allow_agent=False, look_for_keys=False,
timeout=10)
transport=jumphost.get_transport().open_channel(
'direct-tcpip', (self.config['ssh']['host'],
int(self.config['ssh']['port'])), ('', 0)
)

self.sshClient = paramiko.SSHClient()
self.sshClient.load_host_keys(self.config['ssh']['hostKeyFile'])
self.logger.info('trying to connect to builder ' + self.name)
if transport:
if self.jumpClient != None:
transport=self.jumpClient.get_transport().open_channel(
'direct-tcpip', (self.config['ssh']['host'],
int(self.config['ssh']['port'])), ('', 0))
self.sshClient.connect(hostname=self.config['ssh']['host'],
port=int(self.config['ssh']['port']),
username=self.config['ssh']['user'],
Expand Down Expand Up @@ -392,6 +393,11 @@ def runBuild(self):

except Exception as exception:
self.buildLogger.info('build failed: ' + str(exception))
# close out connections since we will retry in this case
if self.sshClient != None:
self.sshClient.close()
if self.jumpClient != None:
self.jumpClient.close()

return (buildSuccess, reschedule)

Expand Down

0 comments on commit 3f6ba19

Please sign in to comment.