Skip to content
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

ensure that storm httpapis tear down runtime tasks if the remote disconnects #1889

Merged
merged 5 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 12 additions & 3 deletions synapse/lib/httpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,16 @@ async def data_received(self, chunk):

class StormNodesV1(Handler):

def on_connection_close(self):
if self.task:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be if self.task is not None no?

self.task.schedCoroSafe(self.task.fini())

async def post(self):
return await self.get()

async def get(self):

self.task = None
user, body = await self.getUserBody()
if body is s_common.novalu:
return
Expand All @@ -313,7 +318,7 @@ async def get(self):
opts = body.get('opts')
query = body.get('query')

await self.cell.boss.promote('storm', user=user, info={'query': query})
self.task = await self.cell.boss.promote('storm', user=user, info={'query': query})

opts = await self._reqValidOpts(opts)

Expand All @@ -324,11 +329,15 @@ async def get(self):

class StormV1(Handler):

def on_connection_close(self):
if self.task:
self.task.schedCoroSafe(self.task.fini())

async def post(self):
return await self.get()

async def get(self):

self.task = None
user, body = await self.getUserBody()
if body is s_common.novalu:
return
Expand All @@ -341,7 +350,7 @@ async def get(self):
opts = await self._reqValidOpts(opts)
opts['editformat'] = 'splices'

await self.cell.boss.promote('storm', user=user, info={'query': query})
self.task = await self.cell.boss.promote('storm', user=user, info={'query': query})

async for mesg in self.cell.storm(query, opts=opts):
self.write(json.dumps(mesg))
Expand Down
36 changes: 36 additions & 0 deletions synapse/tests/test_lib_httpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,42 @@ async def test_http_storm(self):

self.eq(0x01020304, node[0][1])

# Task cancellation during long running storm queries works as intended
body = {'query': '.created | sleep 10'}
task = None
async with sess.get(f'https://localhost:{port}/api/v1/storm', json=body) as resp:

async for byts, x in resp.content.iter_chunks():

if not byts:
break

mesg = json.loads(byts)
if mesg[0] == 'node':
task = core.boss.tasks.get(list(core.boss.tasks.keys())[0])
break

self.nn(task)
self.true(await task.waitfini(10))
self.len(0, core.boss.tasks)

task = None
async with sess.get(f'https://localhost:{port}/api/v1/storm/nodes', json=body) as resp:

async for byts, x in resp.content.iter_chunks():

if not byts:
break

mesg = json.loads(byts)
self.len(2, mesg) # Is if roughly shaped like a node?
task = core.boss.tasks.get(list(core.boss.tasks.keys())[0])
break

self.nn(task)
self.true(await task.waitfini(10))
self.len(0, core.boss.tasks)

# check reqvalidstorm with various queries
tvs = (
('test:str=test', {}, 'ok'),
Expand Down