Design notes

Cylinder is one file. This page is for someone who reads or changes that file. The comments in the file say what the code does. This page says why it is written that way, where the reason is longer than a comment should be: what was measured, and what was considered and not done.

How to use Cylinder is in the guide, and what it promises is in the reference.

A request from start to finish

get_app() returns the function app(), which a WSGI server calls for every request. It:

  1. calls app_map(), which chooses the site

  2. walks from the site down the URL, in find_levels()

  3. finds the files of the early hooks and the page for the request, in get_processors(), and runs none of them

  4. runs the early hooks and then the page, each through process_module()

  5. turns whatever was raised into a response, in handle_error()

  6. runs the late hooks of every level that the walk reached

  7. logs the request and returns the response

The order a request runs in says the same from the outside.

A request is kept for the context that it runs in

What Cylinder knows of a request is on global_proxy, a Werkzeug Local, which keeps it for the context that the request runs in. So find_files() and static_file() read the levels of the request without being passed them, and a log record carries its ID.

What is known of a request stays when app() returns, so that a streamed body and what call_on_close() runs log under its ID.

A name is looked up in a listing

To find the file for a URL, Cylinder never asks the filesystem whether a name is there. It reads the directory, in list_directory(), and looks the name up in what it read.

A filesystem often opens a file by more than one spelling of its name: in another letter case on Windows and macOS, and on Windows by a short name or with a dot added to the end. If a name from a URL were handed to the filesystem, /ADMIN/report could be served without the hook that was written for /admin. A listing has each name once, in the spelling it has on disk. So a file is found by one URL only, and it does not matter which other spellings the filesystem would accept.

Four more things follow from this.

  • Nothing is found through a link. A listing leaves links out. A symbolic link that is not followed is neither a file nor a directory. A junction on Windows is called a directory, so is_windows_link() looks at its tag, which comes with the listing and costs no second question to the disk.

  • The directory that holds a site may be spelled in any way, and nothing below it may. Nothing lists that directory, where every search starts, so site_dir is resolved with os.path.realpath(). The site is the first name that is looked up, in the listing of site_dir.

  • The name of a site is one name, and not a path. site_name is looked up in the listing of site_dir as it is, so a path names nothing there, and is refused as a misspelled name is. A name that names nothing once found nothing: every URL was a 404, with nothing in the log to say why. If the name were joined to site_dir as a path, a site could start below the hooks at its root, or outside site_dir, and the directories in the path would be the one thing that the filesystem is asked about where everything else is looked up in a listing.

  • A request sees one state of each directory. find_levels() reads each directory on the way down once, and everything that is looked up afterwards is looked up in what it read.

The walk stops at the first segment of the URL that is not a directory under that exact name. So the work that a request causes depends on how deep the site is, and not on how long a URL a client sends.

The walk puts each level where the rest of the request reads it as soon as it is found, and the late hooks are looked for after the page has run or failed. So an error on the way down, which a placeholder that is refused is, is answered by the 500 handler and the late hooks of the levels that were reached. Were the levels handed over only when the walk was done, that error would have no file to answer it.

Two details of the walk are there because of Windows:

  • A path is built as a string, and not with os.path.join() or the / of pathlib. Both read a name such as D:foo as a path on another drive, and C:foo as plain foo.

  • os.path.realpath() is called directly. pathlib.Path.resolve() calls it too, and was measured to take half as long again.

What a file server does not send

static_file() sends the file at the path of the URL, unless it is hidden, a link, or a handler file.

A Python file is sent. A file is public only where a handler serves it, so a rule against every Python file guards little that the handler’s place does not, and it keeps a page from loading Python that runs in the browser.

A handler file is not sent, and neither is a copy of one. The one-line file server at the root of a site serves every file in the site. With every Python file let through, it sent the source of the hook at /admin.hook.early.py, and of every page. A section that is mostly files, with one page of its own, is as natural a layout, and its file server sent the source of that page. So a name is not sent if it has the part that says what a handler file is, in any letter case, with .py anywhere after it: handler_source is the check. It once asked for the whole suffix of a handler file, and missed the copy that a file manager makes of a hook, admin.hook.early - Copy.py. A Python file with such a part in its name that is no copy of a handler is not sent either, which is the safe side to be wrong on.

A range is read from its first byte

static_file() reads the part of a file that it sends, and no other part. Nothing is read for a HEAD request or a 304, and a 416 is refused before the file is opened.

Werkzeug cuts a range out of a body by reading the body from its start, unless the body can be told where to start. The file was read that way: where it was measured, a request for the last 864 bytes of a file of 64 megabytes read all of it. So make_conditional() is called first, and the body is set afterwards, from the range that it settled on.

The file is opened before static_file() returns. It was opened when its first byte was asked for, which is after the server has been given the headers of a 200. So a file that could not be opened, as one that another program has locked or that the server’s user may not read, failed in the server: the server answered it, Cylinder’s log said 200, and the 500 handler did not run. The body is a generator that opens the file, and static_file() runs it up to there, so that such a file fails where Cylinder answers it. That opens the file for a HEAD request and a 304 as well, which answer as the GET for the file would.

The file is not handed to Werkzeug open. That is one line in place of the generator, and Werkzeug then goes to the first byte of a range by itself. But the file would stay open until something closed the response. A WSGI server closes it. Werkzeug’s test client does not, and 34 tests left a file open. The generator closes the file once the last byte is read, or when it is closed or thrown away, whether any of the file was read or not.

What is kept between requests

Two things are, and both are speed-ups that change nothing a client can see:

  • the compiled code of each handler file, in get_code(), kept until the modification time or the size of the file changes

  • the listing of each directory, in load_directory(), kept until the modification time of the directory changes

This is what they are worth. The times are for one request that passes two early hooks, a page two directories down, and a late hook, in microseconds, measured in December 2026 on one machine with Python 3.12.

Linux

Windows

As it is

151

527

Handler files compiled for every request

384

1182

Directories listed for every request

205

712

The code is not taken from Python’s own bytecode cache. That cache compares modification times in whole seconds, so it misses an edit that keeps the length of a file and is made within a second of the last one.

Nothing is kept that was read in the last two seconds. A filesystem stamps a change from a clock that moves in steps, and a second change in the same step leaves the time as it was. The step was measured at 4 milliseconds on Linux with ext4 and at under 1 on Windows with NTFS, and it is one or two seconds on some older filesystems. Without the wait, a request that comes between two changes keeps what it read, and the second change is not seen until there is a third. Where a page was added to a directory, then requested, and then its hook was added, the page was served without the hook in 262 of 300 tries on Linux, and in 2 of 300 on Windows. is_settled() is the check.

The module of a handler file is not kept. The code is compiled once and run for every request. A module that was kept would be faster, and a variable at the top of a handler file would then be shared by every request and every user. Data that leaks from one request to another is too easy to write and too hard to notice, so each request gets a module of its own. What belongs at the top of a handler file says what follows from that.

A handler file is run when its turn comes

process_module() runs a file from its first line when the file is about to answer. Every file of a request was once run as it was found: the code outside main() of each hook, of the page, and of each late hook ran before the main() of the first early hook. Three things followed, and each was seen in a test:

  • The top of a page ran for a request that an early hook then turned away.

  • A page that failed there, as one with a syntax error does, was a 500 before any hook had run. A visitor that a hook would have turned away with a 401 was told that the page was broken. No late hook ran on that 500, so a header that one sets on every response was missing from it.

  • An error handler file that failed there got a plain 500, where one whose main() raises is answered by the 500 handler.

Run in its turn, a file fails outside main() where its main() would have failed, and is answered in the same way.

The files are not all run first as a check. That found a late hook that could not be run before the page had changed anything. A late hook whose main() raises is found no sooner, so a page could never count on it.

A file is read when it runs, and the listing was read before. One that is removed in between, while the files before it run, is a 500, as a file that fails is.

An error that cannot be answered

handle_error() always returns a response. Where the 500 handler fails too, or the response for an error cannot be made, it answers with Werkzeug’s page for a 500, which takes the place of the response as any error does.

It once raised there, and app() sent a new response that had nothing of the old one. Three things were lost with the old response, and each was seen in a test: the headers that were set on it, the late hooks, which were skipped, and what was registered with call_on_close(), which never ran. A site that closes its database connection that way left one open for each such request.

app() still has an except of its own. It is for a response that cannot be read at all, as one whose headers were set to a dictionary cannot. Nothing is copied from such a response, and no late hook is given it. What it was told to do when it is closed is still done.

The same except is for a response that cannot be sent. What the server is given is made inside it, with get_wsgi_response(), and not by calling the response afterwards: Werkzeug reads a Location as a URL only when it writes the headers, and raises for one such as //[. That exception once went to the server, with nothing logged and nothing closed.

The server is given the response where that except answers too. A server checks the headers when start_response() is called, and Waitress and wsgiref raise for one that only a server may send, such as Connection. A server writes the headers in Latin-1 after app() has returned, where nothing can answer, so a character that Latin-1 does not have is looked for first. Either once went to the server, which sent a response of its own while the log said 200. A server that has refused a response takes the 500 only with the exception that it raised, as PEP 3333 asks. One that was given no response is not given the exception, because Werkzeug’s test client raises any exception that it is given.

A redirect is made when abort() is called. HTTPRedirect makes its response there, and reads the URL with iri_to_uri(), as Werkzeug does when it writes the headers. So a redirect that cannot be sent fails in the handler, where the 500 handler and the late hooks answer it as any error: one to a URL that Werkzeug cannot read, such as //[, and one to a URL with a line break, which no header can hold. The next of a login form is how a client could send either. Found later, when the response was made or sent, it was a plain 500, and for //[ one without the headers that the late hooks set. The except is left for a Location that a handler sets itself.

Made that way, a redirect is an exception that brings its own response, as the one that abort(response) raises is. answer_error() sends both as they are, and runs no error handler file for either.

A handler file as a module

While the top-level code of a handler file runs, its module is in sys.modules under its __name__. Code that makes a class often looks there for the module of the class. dataclasses does, to read an annotation that is a string, and every annotation is one in a file that starts with from __future__ import annotations. Without the entry, such a dataclass fails on Python 3.13 and older with AttributeError: 'NoneType' object has no attribute '__dict__', and on 3.15 it takes a ClassVar for a field without a word. The entry is taken out again when the code has run.

Two requests can run the top-level code of one file at once, and sys.modules has room for one module under a name. The second module is named for the path with a number after it, such as users.page.get.py#2. Three other ways were looked at:

  • A lock for each file, so that the name is always the path. A lock around code that runs for every request is taken on every request. A hook at the root of a site is on the path of every URL, so one hang at the top of that file would stop the whole site, and a hang that happens once would hold every later request until a restart.

  • A new name for every request. Some things are kept under the name of a module for as long as the process runs, as the logger that logging.getLogger(__name__) makes is. Each request would leave one behind.

  • One name, and no lock. The second request then finds the module of the first, which may not have run as far. A ClassVar was taken for a field that way on Python 3.12 and on 3.15.

Cylinder’s own log lines and errors name a handler file by its __file__, which has no number.

Parameters are read with inspect.signature()

run_func_with_dict() reads the parameters of every main() and app_map() with inspect.signature(), whatever it is: a function, a decorated function, a method, a partial, or an object that can be called.

  • A faster reader for plain functions was taken out. It read the parameters from the function itself. With it, a request like the one in the table above took 25 to 35 microseconds less on Linux, and 75 to 90 less on Windows, measured in a run of its own. It was a second way to do one thing, with a rule of its own for when it applied, and one way is less to read and to get wrong.

  • No annotation is run. Since Python 3.14 an annotation is not run where it is written. inspect.signature() runs it, and fails with a NameError on one that names a type which only a type checker imports, unless it is asked for the annotations as text. signature_options asks for that.

  • signature_options is one expression. The module that names the format is not there before Python 3.14, and an if around an import would leave a line that does not run on one Python or the other. CI asks for every line to run on each Python that it tests with.

What is not asked of Werkzeug

Werkzeug warns about a function for a release or more before it takes the function away. Its development branch for 3.2, as it stood in December 2026, marks three that Cylinder called for removal in 4.0. Each did little, so Cylinder does it itself, in a way that is the same on every Werkzeug:

  • The path of the URL is read from the WSGI environment. It was read with get_path_info(). Werkzeug names request.path in its place, where the slashes at the start are merged into one, and //users is not the URL of /users. The bytes of the path are decoded as Werkzeug decodes them for request.path.

  • Cylinder has its own list of the headers that an error does not carry over, in headers_left_behind. Ten of them came from is_entity_header(), which knows four on that branch. With it, an error would have carried over the Last-Modified of a file that a late hook refused, with no line of Cylinder changed and only a warning to show for it. A header is on the list if it says something about the body that is thrown away, or what to do with it. Werkzeug’s ten did not cover that: Content-Disposition was not among them, and an error was saved as the download that it took the place of.

  • The ETag of a file is set as a header, in the quotes that it is sent in. It was set with Response.set_etag().

static_file() still calls Response.make_conditional(), which the same branch marks for removal in 3.3. The two methods that take its place are in no Werkzeug before 3.2. So the call stays until the oldest Werkzeug that Cylinder allows has them. Werkzeug 3.2 needs Python 3.11, which is the oldest that Cylinder runs on too, so no Python stands in the way of asking for it once it is out.

If-Match is answered by static_file(), and kept from make_conditional(). Werkzeug sets the status 412 and sends the file with it, takes * for a tag that no file has, and does not look at the header for a range that it can serve. The last is the case that the header is sent for: a download in parts, which must not go on with a file that has changed. So static_file() aborts with the 412 first, and calls make_conditional() with the header left out. It reads the header itself, as a list of tags: the method of Werkzeug that compares them has another name on the branch for 3.2. If-Unmodified-Since is answered beside it, where the HTTP standard puts it: Werkzeug reads its date and does nothing with it.

Cylinder allows no Werkzeug that its tests have not run on. pyproject.toml asks for one older than 3.2, which was not out when this was written. A release of Cylinder cannot be changed once it is published. With no upper bound, every installed copy would take the next Werkzeug as it came, and on the one that takes make_conditional() away, static_file() would answer a 500 for every file. The bound is raised in a release of Cylinder with a new minor number, as the reference promises, once the tests pass on the new Werkzeug: the contributing guide says how a new one is noticed.

Logging

  • A record is written by the thread that logs it. Handing each record to a thread of its own was tried, and cost more than writing it. A slow log handler shows what to do for a handler that waits.

  • A log handler keeps a formatter of its own. Cylinder once gave its format to every handler, and the format was changed by replacing cylinder.log_formatter. That was a second place to set what a handler already has a place for, and it took the format away from a handler that came with one, the handler of pytest’s caplog among them. A handler that has none is given Cylinder’s.

  • The URL and the method of a request are logged in ASCII, with everything else percent-encoded. request.url was logged, which Werkzeug decodes. It raises for a Host that Werkzeug cannot make a URL of, such as [::1.2.3.999], and it was read before anything could catch that, so one header from any client sent an exception to the server. A decoded URL also has whatever characters a client chose in it, as a method can, and a log file in an encoding that cannot hold one of them loses the line. A method can hold an escape sequence that a terminal acts on as well: Werkzeug’s development server and wsgiref pass on whatever a client sends.

  • The request fields are added on the handler as well as on the logger. A record from a logger beneath the app’s own, as logger.getChild() makes one, is handed to the handler without passing through a filter of the app’s logger. It had no request_id and no timestamp, so it could not be formatted, and was lost.

  • A request ID is random. It is not taken from the clock, because requests in other threads and worker processes can start in the same instant, and each needs an ID of its own.

  • Times are measured with time.perf_counter(). The system clock can be set while a request runs. time.monotonic() cannot, but before Python 3.13 it moves in steps of about 16 milliseconds on Windows.

What the code is held to

  • It stays small. A test counts the lines of code in src/cylinder.py and fails when they reach the number that the README gives.

  • The tests call get_app() and nothing else in the module. Everything inside can be rearranged without touching a test, and a change that a client could see fails one.

  • CI runs on Linux, Windows, and macOS, because what a name matches is decided by the filesystem.

  • A warning fails a test. pytest.ini turns every warning into an error. So a call that Werkzeug or Python has marked for removal fails a test while it still works for a user. The job for Windows and macOS installs the newest Werkzeug each time it runs, and is where such a call fails first.

The contributing guide has the rest.