Repository: locustio/locust

Issue:
cant show html chart version locust 2.4.0
<!-- 
If you have a general question about how to use Locust, please check Stack Overflow first https://stackoverflow.com/questions/tagged/locust

You can also ask new questions on SO, https://stackoverflow.com/questions/ask just remember to tag your question with "locust". Do not immediately post your issue here after posting to SO, wait for an answer there instead.

Use this form only for reporting actual bugs in Locust. Remember, the developers of Locust are unpaid volunteers, so make sure you have tried everything you can think of before filing a bug :) 

Always make sure you are running an up to date Locust version (pip3 install -U locust)
-->

### Describe the bug
<!-- A clear and concise description of what the bug is -->
Headless mode cannot view charts-html
### Expected behavior
<!-- Tell us what you think should happen -->
The generated htlm can be displayed normally
### Actual behavior
<!-- Tell us what happens instead. Include screenshots if this an issue with the GUI. -->
![image](https://user-images.githubusercontent.com/47803360/138066589-6cfc194d-9fbb-4142-9683-1c54917e8528.png)


### Steps to reproduce
<!-- Please provide a minimal reproducible code example (https://stackoverflow.com/help/minimal-reproducible-example) --> 
`from locust import HttpUser,TaskSet,between,task,FastHttpUser

from locust import LoadTestShape



headers = {"Content-Type": "application/json", "app_id": "101"}
class UserBehavior(TaskSet):

    def on_start(self):
        pass
    @task
    def test_health(self):
        '''查询全部'''

        url = '/statapi/health'

        with self.client.get(url,catch_response=True,timeout=3) as response:
            # print("=====", response)
            if response.status_code!=200:
                response.failure('Failed!')


    def on_stop(self):
        pass




class MyCustomShape(LoadTestShape):


    stages = [
        {"time": 10, "users": 100, "spawn_rate": 50},
        {"time": 30, "users": 200, "spawn_rate": 50},

    ]
    def tick(self):
        run_time = self.get_run_time()
        print("run_time:",run_time)
        for stage in self.stages:

            if run_time < stage['time']:
                print('11111',stage['time'])
                tick_data = (stage['users'],stage['spawn_rate'])
                print('tick_data', tick_data)
                return tick_data
        return None


        # return None
class WebsiteUser(FastHttpUser):
    tasks = [UserBehavior]

    wait_time = between(1, 1)
`
### Environment

- OS:
- Python version:3.9
- Locust version: (please dont file issues for anything but the most recent release or prerelease builds)
- Locust command line that you ran:
- Locust file contents (anonymized if necessary):


Discussion:

Resolving patch:
diff --git a/locust/main.py b/locust/main.py
--- a/locust/main.py
+++ b/locust/main.py
@@ -341,6 +341,8 @@ def spawn_run_time_quit_greenlet():
         stats_printer_greenlet = gevent.spawn(stats_printer(runner.stats))
         stats_printer_greenlet.link_exception(greenlet_exception_handler)
 
+    gevent.spawn(stats_history, runner)
+
     def start_automatic_run():
         if options.master:
             # wait for worker nodes to connect
@@ -408,8 +410,6 @@ def start_automatic_run():
     if options.csv_prefix:
         gevent.spawn(stats_csv_writer.stats_writer).link_exception(greenlet_exception_handler)
 
-    gevent.spawn(stats_history, runner)
-
     def shutdown():
         """
         Shut down locust by firing quitting event, printing/writing stats and exiting


Test patch: