Wait with running on_stop until on_start has finished. This is because on_stop could depend on variables / statuses set during on_start:
def on_start(self):
self.myvar = runSomeRequest()
def on_stop(self):
deleteSomething(self.myvar)
[Edit]
Same case for tasks. The on_stop method would maybe not cleanup resources from the currently running task:
@task
def test(self):
item = randomString()
r = self.client.post('...', data = item)
self.myvar.items.append(item)
def on_stop(self):
for item in items:
self.client.delete('...%s' % item)
I havent validated the behaviour you are seeing, but it definitely looks like a bug, or at least a feature we would want.
This is not supposed to happen within the same Locust I think. If it does happen I'd really like to see a minimal reproducible example.
It's definitely possible that on_stop could be run on Locust User B while the on_start is still executing in Locust User A. That is something that we can'tdo much about since it would requiring synchronization across distributed nodes.
Sorry, I realised what I wrote in the previous message is incorrect. For some reason I thought you meant that on_stop could run before on_start, but I can clearly see that's not what you wrote.
It's correct that with the current implementation we do not wait for on_start to finish when the test is stopped. Instead the Locusts' greenlets are immediately killed which could happen while on_start is still being executed if it's long running, or the test is stopped very quickly after it has been started. I think #1099 might be able to change this behaviour though.
Yes, this is pretty much solved by #1099 . I added a test case to verify it, and will PR it later (it turns out on_start and on_stop are not tested anywhere...)
Most helpful comment
Yes, this is pretty much solved by #1099 . I added a test case to verify it, and will PR it later (it turns out on_start and on_stop are not tested anywhere...)