We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
panel serve --autoreload does not work consistently with imported modules
#### ALL software version info

- python: 3.9.2
    - holoviews: 1.14.4
    - jupyter_bokeh: 3.0.2
    - ipython 7.22.0
    - jupyterlab: 3.0.16
    - pyviz-comms: 2.0.1

- MacOS: 11.4

- VSCode Insiders: 
    - Version: 1.59.0-insider
    - Commit: f6df685c62da50886f7540cbf768ed4333d58bea
    - Date: 2021-07-13T05:15:28.368Z
    - Electron: 12.0.13
    - Chrome: 89.0.4389.128
    - Node.js: 14.16.0
    - V8: 8.9.255.25-electron.0
    - OS: Darwin x64 20.5.0

#### Description of expected behavior and the observed behavior

`panel --autoreload` does not trigger a reload when saving changes to an imported module.

However, if I first save the actual served module (triggering a reload), *then* subsequent saves of imported modules will also trigger reloads. It's as if the "registry" of imports is not populated when you first start serving, and only becomes populated after the first reload.

#### Complete, minimal, self-contained example code that reproduces the issue

```
# test_panel.py
import holoviews as hv
import panel as pn

data = [('a', 10), ('b', 7)]
def test():
    return pn.panel(hv.Bars(data))

# test.py
import test_panel
test_panel.test().servable()
```

Try `panel serve --autoreload` with `test.py`. While serving, change the data values in `test_panel.py` and save. No reload.

Try the same thing again, but this time save `test.py` immediately after you begin serving (you don't have to change anytime, just save it to trigger a server reload). Now if you change the values in `test_panel.py`, it will reload.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `panel/io/reload.py`
Content:
```
1 import fnmatch
2 import os
3 import sys
4 import types
5 
6 from contextlib import contextmanager
7 from functools import partial
8 
9 from .callbacks import PeriodicCallback
10 from .state import state
11 
12 _watched_files = set()
13 _modules = set()
14 _callbacks = {}
15 
16 # List of paths to ignore
17 DEFAULT_FOLDER_BLACKLIST = [
18     "**/.*",
19     "**/anaconda",
20     "**/anaconda2",
21     "**/anaconda3",
22     "**/dist-packages",
23     "**/miniconda",
24     "**/miniconda2",
25     "**/miniconda3",
26     "**/node_modules",
27     "**/pyenv",
28     "**/site-packages",
29     "**/venv",
30     "**/virtualenv",
31 ]
32 
33 
34 def in_blacklist(filepath):
35     return any(
36         file_is_in_folder_glob(filepath, blacklisted_folder)
37         for blacklisted_folder in DEFAULT_FOLDER_BLACKLIST
38     )
39 
40 def file_is_in_folder_glob(filepath, folderpath_glob):
41     """
42     Test whether a file is in some folder with globbing support.
43 
44     Parameters
45     ----------
46     filepath : str
47         A file path.
48     folderpath_glob: str
49         A path to a folder that may include globbing.
50     """
51     # Make the glob always end with "/*" so we match files inside subfolders of
52     # folderpath_glob.
53     if not folderpath_glob.endswith("*"):
54         if folderpath_glob.endswith("/"):
55             folderpath_glob += "*"
56         else:
57             folderpath_glob += "/*"
58 
59     file_dir = os.path.dirname(filepath) + "/"
60     return fnmatch.fnmatch(file_dir, folderpath_glob)
61 
62 def autoreload_watcher():
63     """
64     Installs a periodic callback which checks for changes in watched
65     files and sys.modules.
66     """
67     cb = partial(_reload_on_update, {})
68     _callbacks[state.curdoc] = pcb = PeriodicCallback(callback=cb)
69     pcb.start()
70 
71 def watch(filename):
72     """
73     Add a file to the watch list.
74 
75     All imported modules are watched by default.
76     """
77     _watched_files.add(filename)
78 
79 @contextmanager
80 def record_modules():
81     """
82     Records modules which are currently imported.
83     """
84     modules = set(sys.modules)
85     yield
86     if _modules:
87         return
88     for module_name in set(sys.modules).difference(modules):
89         if module_name.startswith('bokeh_app'):
90             continue
91         module = sys.modules[module_name]
92         try:
93             spec = getattr(module, "__spec__", None)
94             if spec is None:
95                 filepath = getattr(module, "__file__", None)
96                 if filepath is None: # no user
97                     continue
98             else:
99                 filepath = spec.origin
100 
101             filepath = os.path.abspath(filepath)
102 
103             if filepath is None or in_blacklist(filepath):
104                 continue
105 
106             if not os.path.isfile(filepath): # e.g. built-in
107                 continue
108             _modules.add(module_name)
109         except Exception:
110             continue
111 
112 def _reload(module=None):
113     if module is not None:
114         for module in _modules:
115             del sys.modules[module]
116     if state.curdoc in _callbacks:
117         _callbacks[state.curdoc].stop()
118         del _callbacks[state.curdoc]
119     if state.location:
120         state.location.reload = True
121 
122 def _check_file(modify_times, path, module=None):
123     try:
124         modified = os.stat(path).st_mtime
125     except Exception:
126         return
127     if path not in modify_times:
128         modify_times[path] = modified
129         return
130     if modify_times[path] != modified:
131         _reload(module)
132         modify_times[path] = modified
133 
134 def _reload_on_update(modify_times):
135     for module_name in _modules:
136         # Some modules play games with sys.modules (e.g. email/__init__.py
137         # in the standard library), and occasionally this can cause strange
138         # failures in getattr.  Just ignore anything that's not an ordinary
139         # module.
140         if not module_name in sys.modules:
141             continue
142         module = sys.modules[module_name]
143         if not isinstance(module, types.ModuleType):
144             continue
145         path = getattr(module, "__file__", None)
146         if not path:
147             continue
148         if path.endswith(".pyc") or path.endswith(".pyo"):
149             path = path[:-1]
150         _check_file(modify_times, path, module_name)
151     for path in _watched_files:
152         _check_file(modify_times, path)
153
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/panel/io/reload.py b/panel/io/reload.py
--- a/panel/io/reload.py
+++ b/panel/io/reload.py
@@ -113,11 +113,12 @@
     if module is not None:
         for module in _modules:
             del sys.modules[module]
-    if state.curdoc in _callbacks:
-        _callbacks[state.curdoc].stop()
-        del _callbacks[state.curdoc]
-    if state.location:
-        state.location.reload = True
+    for cb in _callbacks.values():
+        cb.stop()
+    _callbacks.clear()
+    state.location.reload = True
+    for loc in state._locations.values():
+        loc.reload = True
 
 def _check_file(modify_times, path, module=None):
     try:

{"golden_diff": "diff --git a/panel/io/reload.py b/panel/io/reload.py\n--- a/panel/io/reload.py\n+++ b/panel/io/reload.py\n@@ -113,11 +113,12 @@\n     if module is not None:\n         for module in _modules:\n             del sys.modules[module]\n-    if state.curdoc in _callbacks:\n-        _callbacks[state.curdoc].stop()\n-        del _callbacks[state.curdoc]\n-    if state.location:\n-        state.location.reload = True\n+    for cb in _callbacks.values():\n+        cb.stop()\n+    _callbacks.clear()\n+    state.location.reload = True\n+    for loc in state._locations.values():\n+        loc.reload = True\n \n def _check_file(modify_times, path, module=None):\n     try:\n", "issue": "panel serve --autoreload does not work consistently with imported modules\n#### ALL software version info\r\n\r\n- python: 3.9.2\r\n    - holoviews: 1.14.4\r\n    - jupyter_bokeh: 3.0.2\r\n    - ipython 7.22.0\r\n    - jupyterlab: 3.0.16\r\n    - pyviz-comms: 2.0.1\r\n\r\n- MacOS: 11.4\r\n\r\n- VSCode Insiders: \r\n    - Version: 1.59.0-insider\r\n    - Commit: f6df685c62da50886f7540cbf768ed4333d58bea\r\n    - Date: 2021-07-13T05:15:28.368Z\r\n    - Electron: 12.0.13\r\n    - Chrome: 89.0.4389.128\r\n    - Node.js: 14.16.0\r\n    - V8: 8.9.255.25-electron.0\r\n    - OS: Darwin x64 20.5.0\r\n\r\n#### Description of expected behavior and the observed behavior\r\n\r\n`panel --autoreload` does not trigger a reload when saving changes to an imported module.\r\n\r\nHowever, if I first save the actual served module (triggering a reload), *then* subsequent saves of imported modules will also trigger reloads. It's as if the \"registry\" of imports is not populated when you first start serving, and only becomes populated after the first reload.\r\n\r\n#### Complete, minimal, self-contained example code that reproduces the issue\r\n\r\n```\r\n# test_panel.py\r\nimport holoviews as hv\r\nimport panel as pn\r\n\r\ndata = [('a', 10), ('b', 7)]\r\ndef test():\r\n    return pn.panel(hv.Bars(data))\r\n\r\n# test.py\r\nimport test_panel\r\ntest_panel.test().servable()\r\n```\r\n\r\nTry `panel serve --autoreload` with `test.py`. While serving, change the data values in `test_panel.py` and save. No reload.\r\n\r\nTry the same thing again, but this time save `test.py` immediately after you begin serving (you don't have to change anytime, just save it to trigger a server reload). Now if you change the values in `test_panel.py`, it will reload.\n", "before_files": [{"content": "import fnmatch\nimport os\nimport sys\nimport types\n\nfrom contextlib import contextmanager\nfrom functools import partial\n\nfrom .callbacks import PeriodicCallback\nfrom .state import state\n\n_watched_files = set()\n_modules = set()\n_callbacks = {}\n\n# List of paths to ignore\nDEFAULT_FOLDER_BLACKLIST = [\n    \"**/.*\",\n    \"**/anaconda\",\n    \"**/anaconda2\",\n    \"**/anaconda3\",\n    \"**/dist-packages\",\n    \"**/miniconda\",\n    \"**/miniconda2\",\n    \"**/miniconda3\",\n    \"**/node_modules\",\n    \"**/pyenv\",\n    \"**/site-packages\",\n    \"**/venv\",\n    \"**/virtualenv\",\n]\n\n\ndef in_blacklist(filepath):\n    return any(\n        file_is_in_folder_glob(filepath, blacklisted_folder)\n        for blacklisted_folder in DEFAULT_FOLDER_BLACKLIST\n    )\n\ndef file_is_in_folder_glob(filepath, folderpath_glob):\n    \"\"\"\n    Test whether a file is in some folder with globbing support.\n\n    Parameters\n    ----------\n    filepath : str\n        A file path.\n    folderpath_glob: str\n        A path to a folder that may include globbing.\n    \"\"\"\n    # Make the glob always end with \"/*\" so we match files inside subfolders of\n    # folderpath_glob.\n    if not folderpath_glob.endswith(\"*\"):\n        if folderpath_glob.endswith(\"/\"):\n            folderpath_glob += \"*\"\n        else:\n            folderpath_glob += \"/*\"\n\n    file_dir = os.path.dirname(filepath) + \"/\"\n    return fnmatch.fnmatch(file_dir, folderpath_glob)\n\ndef autoreload_watcher():\n    \"\"\"\n    Installs a periodic callback which checks for changes in watched\n    files and sys.modules.\n    \"\"\"\n    cb = partial(_reload_on_update, {})\n    _callbacks[state.curdoc] = pcb = PeriodicCallback(callback=cb)\n    pcb.start()\n\ndef watch(filename):\n    \"\"\"\n    Add a file to the watch list.\n\n    All imported modules are watched by default.\n    \"\"\"\n    _watched_files.add(filename)\n\n@contextmanager\ndef record_modules():\n    \"\"\"\n    Records modules which are currently imported.\n    \"\"\"\n    modules = set(sys.modules)\n    yield\n    if _modules:\n        return\n    for module_name in set(sys.modules).difference(modules):\n        if module_name.startswith('bokeh_app'):\n            continue\n        module = sys.modules[module_name]\n        try:\n            spec = getattr(module, \"__spec__\", None)\n            if spec is None:\n                filepath = getattr(module, \"__file__\", None)\n                if filepath is None: # no user\n                    continue\n            else:\n                filepath = spec.origin\n\n            filepath = os.path.abspath(filepath)\n\n            if filepath is None or in_blacklist(filepath):\n                continue\n\n            if not os.path.isfile(filepath): # e.g. built-in\n                continue\n            _modules.add(module_name)\n        except Exception:\n            continue\n\ndef _reload(module=None):\n    if module is not None:\n        for module in _modules:\n            del sys.modules[module]\n    if state.curdoc in _callbacks:\n        _callbacks[state.curdoc].stop()\n        del _callbacks[state.curdoc]\n    if state.location:\n        state.location.reload = True\n\ndef _check_file(modify_times, path, module=None):\n    try:\n        modified = os.stat(path).st_mtime\n    except Exception:\n        return\n    if path not in modify_times:\n        modify_times[path] = modified\n        return\n    if modify_times[path] != modified:\n        _reload(module)\n        modify_times[path] = modified\n\ndef _reload_on_update(modify_times):\n    for module_name in _modules:\n        # Some modules play games with sys.modules (e.g. email/__init__.py\n        # in the standard library), and occasionally this can cause strange\n        # failures in getattr.  Just ignore anything that's not an ordinary\n        # module.\n        if not module_name in sys.modules:\n            continue\n        module = sys.modules[module_name]\n        if not isinstance(module, types.ModuleType):\n            continue\n        path = getattr(module, \"__file__\", None)\n        if not path:\n            continue\n        if path.endswith(\".pyc\") or path.endswith(\".pyo\"):\n            path = path[:-1]\n        _check_file(modify_times, path, module_name)\n    for path in _watched_files:\n        _check_file(modify_times, path)\n", "path": "panel/io/reload.py"}], "after_files": [{"content": "import fnmatch\nimport os\nimport sys\nimport types\n\nfrom contextlib import contextmanager\nfrom functools import partial\n\nfrom .callbacks import PeriodicCallback\nfrom .state import state\n\n_watched_files = set()\n_modules = set()\n_callbacks = {}\n\n# List of paths to ignore\nDEFAULT_FOLDER_BLACKLIST = [\n    \"**/.*\",\n    \"**/anaconda\",\n    \"**/anaconda2\",\n    \"**/anaconda3\",\n    \"**/dist-packages\",\n    \"**/miniconda\",\n    \"**/miniconda2\",\n    \"**/miniconda3\",\n    \"**/node_modules\",\n    \"**/pyenv\",\n    \"**/site-packages\",\n    \"**/venv\",\n    \"**/virtualenv\",\n]\n\n\ndef in_blacklist(filepath):\n    return any(\n        file_is_in_folder_glob(filepath, blacklisted_folder)\n        for blacklisted_folder in DEFAULT_FOLDER_BLACKLIST\n    )\n\ndef file_is_in_folder_glob(filepath, folderpath_glob):\n    \"\"\"\n    Test whether a file is in some folder with globbing support.\n\n    Parameters\n    ----------\n    filepath : str\n        A file path.\n    folderpath_glob: str\n        A path to a folder that may include globbing.\n    \"\"\"\n    # Make the glob always end with \"/*\" so we match files inside subfolders of\n    # folderpath_glob.\n    if not folderpath_glob.endswith(\"*\"):\n        if folderpath_glob.endswith(\"/\"):\n            folderpath_glob += \"*\"\n        else:\n            folderpath_glob += \"/*\"\n\n    file_dir = os.path.dirname(filepath) + \"/\"\n    return fnmatch.fnmatch(file_dir, folderpath_glob)\n\ndef autoreload_watcher():\n    \"\"\"\n    Installs a periodic callback which checks for changes in watched\n    files and sys.modules.\n    \"\"\"\n    cb = partial(_reload_on_update, {})\n    _callbacks[state.curdoc] = pcb = PeriodicCallback(callback=cb)\n    pcb.start()\n\ndef watch(filename):\n    \"\"\"\n    Add a file to the watch list.\n\n    All imported modules are watched by default.\n    \"\"\"\n    _watched_files.add(filename)\n\n@contextmanager\ndef record_modules():\n    \"\"\"\n    Records modules which are currently imported.\n    \"\"\"\n    modules = set(sys.modules)\n    yield\n    if _modules:\n        return\n    for module_name in set(sys.modules).difference(modules):\n        if module_name.startswith('bokeh_app'):\n            continue\n        module = sys.modules[module_name]\n        try:\n            spec = getattr(module, \"__spec__\", None)\n            if spec is None:\n                filepath = getattr(module, \"__file__\", None)\n                if filepath is None: # no user\n                    continue\n            else:\n                filepath = spec.origin\n\n            filepath = os.path.abspath(filepath)\n\n            if filepath is None or in_blacklist(filepath):\n                continue\n\n            if not os.path.isfile(filepath): # e.g. built-in\n                continue\n            _modules.add(module_name)\n        except Exception:\n            continue\n\ndef _reload(module=None):\n    if module is not None:\n        for module in _modules:\n            del sys.modules[module]\n    for cb in _callbacks.values():\n        cb.stop()\n    _callbacks.clear()\n    state.location.reload = True\n    for loc in state._locations.values():\n        loc.reload = True\n\ndef _check_file(modify_times, path, module=None):\n    try:\n        modified = os.stat(path).st_mtime\n    except Exception:\n        return\n    if path not in modify_times:\n        modify_times[path] = modified\n        return\n    if modify_times[path] != modified:\n        _reload(module)\n        modify_times[path] = modified\n\ndef _reload_on_update(modify_times):\n    for module_name in _modules:\n        # Some modules play games with sys.modules (e.g. email/__init__.py\n        # in the standard library), and occasionally this can cause strange\n        # failures in getattr.  Just ignore anything that's not an ordinary\n        # module.\n        if not module_name in sys.modules:\n            continue\n        module = sys.modules[module_name]\n        if not isinstance(module, types.ModuleType):\n            continue\n        path = getattr(module, \"__file__\", None)\n        if not path:\n            continue\n        if path.endswith(\".pyc\") or path.endswith(\".pyo\"):\n            path = path[:-1]\n        _check_file(modify_times, path, module_name)\n    for path in _watched_files:\n        _check_file(modify_times, path)\n", "path": "panel/io/reload.py"}]}