Skip to content

Commit

Permalink
Fixing too many files open bug (#1501)
Browse files Browse the repository at this point in the history
Needed to maintain and close the docker client connection
  • Loading branch information
Sheng Lundquist authored May 29, 2024
1 parent d0dd965 commit 6cd4fa1
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/agent0/core/hyperdrive/interactive/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import docker
import numpy as np
import pandas as pd
from docker import DockerClient
from docker.errors import NotFound
from docker.models.containers import Container
from numpy.random._generator import Generator
Expand Down Expand Up @@ -156,7 +157,8 @@ def __init__(self, rpc_uri: str, config: Config | None = None):
self.chain_id = str(config.db_port)
obj_name = type(self).__name__.lower()
db_container_name = f"agent0-{obj_name}-{self.chain_id}"
self.postgres_config, self.postgres_container = self._initialize_postgres_container(

self.docker_client, self.postgres_config, self.postgres_container = self._initialize_postgres_container(
db_container_name, config.db_port, config.remove_existing_db_container
)
assert isinstance(self.postgres_container, Container)
Expand All @@ -182,7 +184,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):

def _initialize_postgres_container(
self, container_name: str, db_port: int, remove_existing_db_container: bool
) -> tuple[PostgresConfig, Container]:
) -> tuple[DockerClient, PostgresConfig, Container]:
# Attempt to use the default socket if it exists
try:
client = docker.from_env()
Expand Down Expand Up @@ -242,30 +244,45 @@ def _initialize_postgres_container(
)
assert isinstance(container, Container)

return postgres_config, container
return client, postgres_config, container

def cleanup(self):
"""General cleanup of resources of interactive hyperdrive."""
db_engine = None
if self.db_session is not None:
db_engine = self.db_session.get_bind()

try:
if self.db_session is not None:
self.db_session.close()
except Exception: # pylint: disable=broad-except
pass

try:
if db_engine is not None:
db_engine.dispose() # type: ignore
except Exception: # pylint: disable=broad-except
pass

try:
self.postgres_container.kill()
except Exception: # pylint: disable=broad-except
pass

try:
close_logging()
self.postgres_container.remove()
except Exception: # pylint: disable=broad-except
pass

# def __del__(self):
# """General cleanup of resources of interactive hyperdrive."""
# with contextlib.suppress(Exception):
# self.cleanup()
try:
self.docker_client.close()
except Exception: # pylint: disable=broad-except
pass

try:
close_logging()
except Exception: # pylint: disable=broad-except
pass

def block_number(self) -> int:
"""Get the current block number on the chain.
Expand Down

0 comments on commit 6cd4fa1

Please sign in to comment.