Repository: sphinx-doc/sphinx (Python)

Issue:
Smaller/wrong search index with sphinx-build -j auto
### Describe the bug

Using the latest version and building with ````-j auto```` results in a considerably smaller searchindex,js and as a result most searches returning nothing.  If I leave out ````-j```` then the  searchindex is considerably larger and searches work as expected.

Some quick testing showed that Sphinx 6.0.0 did not have this problem while Sphinx 6.1.0 onwards does.

### How to Reproduce

This fails:

    sphinx-build -j auto  -n -b html -d build/doctrees -q -E . build/html

This works:

    sphinx-build -n -b html -d build/doctrees -q -E . build/html

### Environment Information

```text
Platform:              linux; (Linux-5.19.0-29-generic-x86_64-with-glibc2.36)
Python version:        3.10.7 (main, Nov 24 2022, 19:45:47) [GCC 12.2.0])
Python implementation: CPython
Sphinx version:        6.1.3
Docutils version:      0.19
Jinja2 version:        3.0.3
Pygments version:      2.14.0
```


### Sphinx extensions

_No response_

### Additional context

Sphinx conf.py (almost empty!) and other files are at https://github.com/rogerbinns/apsw/tree/master/doc


Discussion:
im also hitting this and can confirm that downgrading to 6.0.1 resolves
I created a fix here: https://github.com/sphinx-doc/sphinx/pull/11192



Patch:
diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py
index 4b067eadb7c..81fac5870e1 100644
--- a/sphinx/builders/html/__init__.py
+++ b/sphinx/builders/html/__init__.py
@@ -655,10 +655,6 @@ def get_doc_context(self, docname: str, body: str, metatags: str) -> dict[str, A
         }
 
     def write_doc(self, docname: str, doctree: nodes.document) -> None:
-        title_node = self.env.longtitles.get(docname)
-        title = self.render_partial(title_node)['title'] if title_node else ''
-        self.index_page(docname, doctree, title)
-
         destination = StringOutput(encoding='utf-8')
         doctree.settings = self.docsettings
 
@@ -678,6 +674,9 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None:
     def write_doc_serialized(self, docname: str, doctree: nodes.document) -> None:
         self.imgpath = relative_uri(self.get_target_uri(docname), self.imagedir)
         self.post_process_images(doctree)
+        title_node = self.env.longtitles.get(docname)
+        title = self.render_partial(title_node)['title'] if title_node else ''
+        self.index_page(docname, doctree, title)
 
     def finish(self) -> None:
         self.finish_tasks.add_task(self.gen_indices)


Test patch:
diff --git a/tests/test_search.py b/tests/test_search.py
index 0fafa297553..d0172df6bbc 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -286,3 +286,10 @@ def test_nosearch(app):
     assert 'latex' not in index['terms']
     assert 'zfs' in index['terms']
     assert index['terms']['zfs'] == []  # zfs on nosearch.rst is not registered to index
+
+
+@pytest.mark.sphinx(testroot='search', parallel=3, freshenv=True)
+def test_parallel(app):
+    app.build()
+    index = load_searchindex(app.outdir / 'searchindex.js')
+    assert index['docnames'] == ['index', 'nosearch', 'tocitem']