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

Overwriting weight by config-users may lead to crash #2852

Closed
2 tasks done
evanchaoli opened this issue Aug 15, 2024 · 4 comments · Fixed by #2891
Closed
2 tasks done

Overwriting weight by config-users may lead to crash #2852

evanchaoli opened this issue Aug 15, 2024 · 4 comments · Fixed by #2891
Labels

Comments

@evanchaoli
Copy link

evanchaoli commented Aug 15, 2024

Prerequisites

Description

In the attached locustfile, there are no "weight" defined for both user classes, which works fine.

However, if I overwrite weights with config-users options, it will crash:

$ locust -u 2 --config-users '[{"user_class_name": "CBotApiUser", "weight": 0}]'
[2024-08-15 11:15:41,053] C02GM2X6MD6R/INFO/locust.main: Starting web interface at http://0.0.0.0:8089
init: RevisionApiUser, 1
init: CBotApiUser, 0
[2024-08-15 11:15:41,071] C02GM2X6MD6R/INFO/locust.main: Starting Locust 2.31.2
[2024-08-15 11:15:47,674] C02GM2X6MD6R/INFO/locust.runners: Ramping to 2 users at a rate of 1.00 per second
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 544, in <lambda>
    lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 493, in _start
    for dispatched_users in self._users_dispatcher:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 133, in __next__
    users_on_workers = next(self._dispatcher_generator)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 168, in _dispatcher
    yield self._add_users_on_workers()
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 300, in _add_users_on_workers
    for user in self._user_generator:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 388, in _user_gen
    yield next(weighted_users_gen)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in _kl_generator
    heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in <listcomp>
    heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
ValueError: math domain error
2024-08-15T03:15:47Z <Greenlet at 0x10ff7d900: <lambda>> failed with ValueError

[2024-08-15 11:15:47,694] C02GM2X6MD6R/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x10ff7d900: <lambda>>
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 544, in <lambda>
    lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 493, in _start
    for dispatched_users in self._users_dispatcher:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 133, in __next__
    users_on_workers = next(self._dispatcher_generator)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 168, in _dispatcher
    yield self._add_users_on_workers()
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 300, in _add_users_on_workers
    for user in self._user_generator:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 388, in _user_gen
    yield next(weighted_users_gen)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in _kl_generator
    heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 32, in <listcomp>
    heap = [(x * log2(x / (x + 1.0)), x + 1.0, x, name) for name, x in users]
ValueError: math domain error
KeyboardInterrupt

As you can see, I use --config-users to overwrite the second user's weight to 0, then it crashed.

Command line

$ locust -u 2 --config-users '[{"user_class_name": "CBotApiUser", "weight": 0}]'

Locustfile contents

from locust import HttpUser, task, tag, constant, events, constant_pacing


@events.init.add_listener
def on_locust_init(environment, **kwargs):
    for n, u in environment.available_user_classes.items():
        print(f'init: {n}, {u.weight}')


class RevisionApiUser(HttpUser):
    wait_time = constant_pacing(5)

    def __init__(self, environment):
        super().__init__(environment)
        self.first = True

    def on_start(self) -> None:
        print(f"revision: {self.weight}")

    @task
    def get_commit_list(self):
        if self.first:
            print(f"revision: {self.weight}")
            self.first = False


class CBotApiUser(HttpUser):
    wait_time = constant_pacing(5)

    def on_start(self) -> None:
        print(f"cbot: {self.weight}")

    def __init__(self, environment):
        super().__init__(environment)
        self.first = True

    @task
    def get_commits(self):
        if self.first:
            print(f"cbot: {self.weight}")
            self.first = False

Python version

3.9.19

Locust version

locust 2.31.2 from /Users/chaol/venv/3.9/lib/python3.9/site-packages/locust (Python 3.9.19, OpenSSL 3.3.0)

Operating system

MacOS

@evanchaoli evanchaoli added the bug label Aug 15, 2024
@cyberw
Copy link
Collaborator

cyberw commented Aug 15, 2024

Interesting. @tdadela as this is your code, do you think you could take a look?

@evanchaoli Is this issue present in 2.30.0?

@evanchaoli
Copy link
Author

@evanchaoli Is this issue present in 2.30.0?

I am new to Locust, only tried the latest version. But I can do a quick test today.

@evanchaoli
Copy link
Author

2.3.0 has the same problem:

$ locust -f locustfile.py  -u 2 --config-users '[{"user_class_name": "CBotApiUser", "weight": 0}]'
[2024-08-16 09:35:03,909] C02GM2X6MD6R/INFO/locust.main: Starting web interface at http://0.0.0.0:8089
init: RevisionApiUser, 1
init: CBotApiUser, 0
[2024-08-16 09:35:03,923] C02GM2X6MD6R/INFO/locust.main: Starting Locust 2.30.0
[2024-08-16 09:35:20,175] C02GM2X6MD6R/INFO/locust.runners: Ramping to 2 users at a rate of 1.00 per second
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 542, in <lambda>
    lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 491, in _start
    for dispatched_users in self._users_dispatcher:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 143, in __next__
    users_on_workers = next(self._dispatcher_generator)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 178, in _dispatcher
    yield self._add_users_on_workers()
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 310, in _add_users_on_workers
    for user in self._user_generator:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 409, in _user_gen
    yield next(weighted_users_gen)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 44, in _kl_generator
    heap = [(x * log2(x / (x + 1.0)), i) for i, x in enumerate(generated)]
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 44, in <listcomp>
    heap = [(x * log2(x / (x + 1.0)), i) for i, x in enumerate(generated)]
ValueError: math domain error
2024-08-16T01:35:20Z <Greenlet at 0x10dac7860: <lambda>> failed with ValueError

[2024-08-16 09:35:20,180] C02GM2X6MD6R/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x10dac7860: <lambda>>
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 542, in <lambda>
    lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/runners.py", line 491, in _start
    for dispatched_users in self._users_dispatcher:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 143, in __next__
    users_on_workers = next(self._dispatcher_generator)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 178, in _dispatcher
    yield self._add_users_on_workers()
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 310, in _add_users_on_workers
    for user in self._user_generator:
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 409, in _user_gen
    yield next(weighted_users_gen)
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 44, in _kl_generator
    heap = [(x * log2(x / (x + 1.0)), i) for i, x in enumerate(generated)]
  File "/Users/chaol/venv/3.9/lib/python3.9/site-packages/locust/dispatch.py", line 44, in <listcomp>
    heap = [(x * log2(x / (x + 1.0)), i) for i, x in enumerate(generated)]
ValueError: math domain error
KeyboardInterrupt

@cyberw
Copy link
Collaborator

cyberw commented Aug 16, 2024

tdadela said he’d get back to me, but it might be a few days. thanks for testing the old version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants