# Buggy code
def start_hatching(self, locust_count, hatch_rate):
    num_slaves = (
        len(self.clients.ready) + len(self.clients.running) + len(self.clients.hatching)
    )
    if not num_slaves:
        logger.warning(
            "You are running in distributed mode but have no slave servers connected. "
            "Please connect slaves prior to swarming."
        )
        return

    self.num_clients = locust_count
    self.hatch_rate = hatch_rate
    slave_num_clients = locust_count // (num_slaves or 1)
    slave_hatch_rate = float(hatch_rate) / (num_slaves or 1)
    remaining = locust_count % num_slaves

    logger.info(
        "Sending hatch jobs of %d locusts and %.2f hatch rate to %d ready clients"
        % (slave_num_clients, slave_hatch_rate, num_slaves)
    )

    if self.state != STATE_RUNNING and self.state != STATE_HATCHING:
        self.stats.clear_all()
        self.exceptions = {}
        events.master_start_hatching.fire()

    for client in self.clients.ready + self.clients.running + self.clients.hatching:
        data = {
            "hatch_rate": slave_hatch_rate,
            "num_clients": slave_num_clients,
            "host": self.host,
            "stop_timeout": self.options.stop_timeout,
        }

        if remaining > 0:
            data["num_clients"] += 1
            remaining -= 1

        self.server.send_to_client(Message("hatch", data, client.id))

    self.state = STATE_HATCHING


# Traceback (RuntimeError)
Traceback (most recent call last):
File "gevent/pywsgi.py", line 964, in handle_one_response
self.run_application()
File "gevent/pywsgi.py", line 911, in run_application
self.result = self.application(self.environ, self.start_response)
File "flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "flask/_compat.py", line 39, in reraise
raise value
File "flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "flask/_compat.py", line 39, in reraise
raise value
File "flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "locust/web.py", line 85, in stop
runners.locust_runner.stop()
File "locust/runners.py", line 196, in stop
for locust_greenlet in self.locusts:
RuntimeError: Set changed size during iteration
2019-11-27T18:41:36Z
{'REMOTE_..., (hidden keys: 26)} failed with RuntimeError

# Fixed code
def start_hatching(self, locust_count, hatch_rate):
    num_slaves = (
        len(self.clients.ready) + len(self.clients.running) + len(self.clients.hatching)
    )
    if not num_slaves:
        logger.warning(
            "You are running in distributed mode but have no slave servers connected. "
            "Please connect slaves prior to swarming."
        )
        return

    self.num_clients = locust_count
    self.hatch_rate = hatch_rate
    slave_num_clients = locust_count // (num_slaves or 1)
    slave_hatch_rate = float(hatch_rate) / (num_slaves or 1)
    remaining = locust_count % num_slaves

    logger.info(
        "Sending hatch jobs of %d locusts and %.2f hatch rate to %d ready clients"
        % (slave_num_clients, slave_hatch_rate, num_slaves)
    )

    if slave_hatch_rate > 100:
        logger.warning(
            "Your selected hatch rate is very high (>100/slave), and this is known to sometimes cause issues. Do you really need to ramp up that fast?"
        )

    if self.state != STATE_RUNNING and self.state != STATE_HATCHING:
        self.stats.clear_all()
        self.exceptions = {}
        events.master_start_hatching.fire()

    for client in self.clients.ready + self.clients.running + self.clients.hatching:
        data = {
            "hatch_rate": slave_hatch_rate,
            "num_clients": slave_num_clients,
            "host": self.host,
            "stop_timeout": self.options.stop_timeout,
        }

        if remaining > 0:
            data["num_clients"] += 1
            remaining -= 1

        self.server.send_to_client(Message("hatch", data, client.id))

    self.state = STATE_HATCHING