Skip to content

Commit

Permalink
Don't try to close the aiohttp session if connector_owner is False (#382
Browse files Browse the repository at this point in the history
)
  • Loading branch information
leszekhanusz authored Jan 30, 2023
1 parent f28670d commit 2981ce3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
21 changes: 15 additions & 6 deletions gql/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,21 @@ async def close(self) -> None:

log.debug("Closing transport")

closed_event = self.create_aiohttp_closed_event(self.session)
await self.session.close()
try:
await asyncio.wait_for(closed_event.wait(), self.ssl_close_timeout)
except asyncio.TimeoutError:
pass
if (
self.client_session_args
and self.client_session_args.get("connector_owner") is False
):

log.debug("connector_owner is False -> not closing connector")

else:
closed_event = self.create_aiohttp_closed_event(self.session)
await self.session.close()
try:
await asyncio.wait_for(closed_event.wait(), self.ssl_close_timeout)
except asyncio.TimeoutError:
pass

self.session = None

async def execute(
Expand Down
44 changes: 44 additions & 0 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,3 +1441,47 @@ async def handler(request):
# Checking that there is no space after the colon in the log
expected_log = '"query":"query getContinents'
assert expected_log in caplog.text


@pytest.mark.asyncio
async def test_aiohttp_connector_owner_false(event_loop, aiohttp_server):
from aiohttp import web, TCPConnector
from gql.transport.aiohttp import AIOHTTPTransport

async def handler(request):
return web.Response(
text=query1_server_answer,
content_type="application/json",
)

app = web.Application()
app.router.add_route("POST", "/", handler)
server = await aiohttp_server(app)

url = server.make_url("/")

connector = TCPConnector()
transport = AIOHTTPTransport(
url=url,
timeout=10,
client_session_args={
"connector": connector,
"connector_owner": False,
},
)

for _ in range(2):
async with Client(transport=transport) as session:

query = gql(query1_str)

# Execute query asynchronously
result = await session.execute(query)

continents = result["continents"]

africa = continents[0]

assert africa["code"] == "AF"

await connector.close()

0 comments on commit 2981ce3

Please sign in to comment.