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

Treat exceptions in init event handler as fatal #3009

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
3 changes: 3 additions & 0 deletions locust/main.py
Original file line number Diff line number Diff line change
@@ -544,6 +544,9 @@ def assign_equal_weights(environment, **kwargs):
# Fire locust init event which can be used by end-users' code to run setup code that
# need access to the Environment, Runner or WebUI.
environment.events.init.fire(environment=environment, runner=runner, web_ui=web_ui)
if log.unhandled_greenlet_exception:
# treat exceptions in init handlers as fatal. They are already logged so no need to log anything more.
sys.exit(1)

if web_ui:
web_ui.start()
33 changes: 33 additions & 0 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
@@ -1442,6 +1442,39 @@ def my_task(self):
# ensure stats printer printed at least one report before shutting down and that there was a final report printed as well
self.assertRegex(stderr, r".*Aggregated[\S\s]*Shutting down[\S\s]*Aggregated.*")

def test_exception_in_init_event(self):
with mock_locustfile(
content=textwrap.dedent(
"""
from locust import User, task, constant, events
@events.init.add_listener
def _(*args, **kw):
raise Exception("something went wrong")

class TestUser(User):
wait_time = constant(10)
@task
def my_task(self):
pass
"""
)
) as mocked:
proc = subprocess.Popen(
[
"locust",
"-f",
mocked.file_path,
"--host",
"https://test.com/",
],
stdout=PIPE,
stderr=PIPE,
text=True,
)
stdout, stderr = proc.communicate(timeout=3)
self.assertIn("something went wrong", stderr)
self.assertEqual(1, proc.returncode)


class DistributedIntegrationTests(ProcessIntegrationTest):
failed_port_check = False