Skip to content

Don't try to close the aiohttp session if connector_owner is False #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()