sphinx-doc / sphinx-doc/sphinx

Move away from `pickle`-based serialisation

Open
#13,612 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type:proposal
Dominant language
Python
Stars
8k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Is your feature request related to a problem? Please describe.

Currently, Sphinx uses Python's pickle module for various serialisation-related functions:

  • Saving and restoring the build environment (app.env) for incremental rebuilds
  • Saving and restoring the Docutils AST ('doctree') of individual documents between the reading and writing phases
  • Reconcilling changes in the build environment between worker processes and the main process, when using parallel processing
  • Saving coverage data in sphinx.ext.coverage

The pickle module is convenient, but also insecure (unpickling can execute arbitrary Python code) and hard to introspect. I would like to replace our use of pickle with data-only serialisation.

Challenges include, but are not limited to:

  • There is no round-trip serialisation format for Docutils ASTs.
  • Extensions currently store arbitrary data directly on app.env, which includes arbitrary (non-builtin) types.

For non-pickle serialisation to be supported by extensions, I expect we will need to provide (a) hooks for serialising and deserialising and (b) an 'official' method of saving data to the environment.

In terms of implementation, we progressively convert attributes or data to be (de)serialised via a non-pickle mechanism.

Describe the solution you'd like

To eventually deprecate and remove all use of the pickle module.

Describe alternatives you've considered

The status quo.

A

cc @sphinx-doc/developers @sphinx-doc/triagers for input.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by auditing Sphinx's pickle use for app.env, individual document doctrees, parallel-processing reconciliation, and sphinx.ext.coverage. Done means these paths progressively use data-only serialisation, with hooks for extensions and an official way to save environment data, eventually allowing pickle to be deprecated and removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.