Skip to content

diskcache-oxide 0.5.8

Latest

Choose a tag to compare

@mikeckennedy mikeckennedy released this 23 Nov 03:44
· 11 commits to main since this release

No format change: caches made by 0.3.5 and later open as they are. One behavior change, for a child process forked at the wrong moment, and fixes from the same review as 0.4.7 that touch the engine's core.

Changed

  • A child forked inside a transaction, or from inside a cache call that has its connection out, can't use that cache at all. Every call on it in that child raises the new ForkedInsideTransactionError, a RuntimeError, at once, reads included, for the life of the process, on a durable=True handle too; close() still works, other caches are unaffected, and on a FanoutCache only the shard involved is refused. Before, a child forked inside with cache.transact(): could still read, and one forked from a pickle hook or a decode function reading a small value inside a call could read and write. Both were unsafe: such a child holds none of the file locks SQLite believes it holds, so once the parent closed, its reads went stale and its set calls returned True and were lost, with no error and nothing for check() to find. Fork before or after the block, or start workers with spawn or forkserver (D-020; see Concurrency and forks). The child logs one warning on the diskcache_oxide logger naming the directories.
  • A Caches lookup and a durable=True Django alias raise ForkedInsideTransactionError in such a child, where any other failed open hands out a NullCache and retries. No retry can help that child, and a silent NullCache would hide a fork in the wrong place for the life of the worker.
  • incr and decr raise ValueError for a delta or default that is an infinity or a NaN (D-064). A counter that reached NaN was stored with no value, so get raised CorruptValueError and the next incr raised TypeError. A counter left that way by an earlier version stays unreadable until it's overwritten or deleted.
  • The error this database can't be written in a process forked inside a transaction is gone: that child now gets the error above for every call.

Fixed

  • Another library's before-fork hook that used a cache (one registered before diskcache_oxide was imported, so it runs after ours) left its connection open across the fork, and the child then lost its writes once the parent closed. A connection given back between the fork hooks is closed now, and that child works.
  • A child forked while another thread was leaving the forking thread's transaction (__exit__ called from a second thread) kept a lock level nothing could release: os.fork() from another thread of that child hung, and leaving the block there waited out the timeout. It raises RuntimeError at once now, and later forks go ahead.
  • One row with a damaged filename or size column (the wrong SQLite type, as a bad page or an edit by hand leaves it) made every get, delete, clear() and check(fix=True) that reached it raise OSError: Invalid column type, and once it was the oldest row, every set that had to evict. A row with a damaged filename, or a damaged size on a value stored in a file, reads as a miss and is removed like any other, and check() reports a row with a wrong-typed mode, size, store_seq, expire_time or filename (damaged row N: its COLUMN is stored as TYPE), which fix=True removes.
  • One row with a damaged store_seq column decided every later sequence number. Text or a BLOB there gave each later write on any key the same number, so clear() could return 0 and leave entries behind, and pop and the queue calls lost their exactly-once footing; a large REAL made every set, expire(), cull() and clear() raise. The next number is worked out from the sound ones only now, so such a row affects only the calls that reach it (by its key, or as the head of a queue) until check(fix=True) removes it. check() also reports a store_seq of 2^62 - 1 or more as damaged.
  • A damaged next sequence number in the cache's one state row (text, a BLOB, a REAL, or 2^62 or more) wasn't reported by check() and wasn't repaired, and with a fractional REAL there incr on a stored number wrote that REAL into the entry's own row, after which reading the entry raised. Every write fails with OSError on such a cache now, clear(), evict, expire() and cull() still run, and check() reports it (damaged cache state: its COLUMN is stored as TYPE); check(fix=True) sets the number past every entry's. Deleting an entry whose own number was the last one below 2^62 left the cache unwritable with nothing to repair it; check() reports that entry as damaged and fix=True repairs both.
  • check() raised OSError instead of reporting when a stored total (count or size in state) was of another type, or when an entry's size was an integer large enough to overflow the sum, and removing an entry with a hugely negative size turned the stored total into a REAL that every later volume(), set and check() raised on. A size below 0 or above 2^48 is a damaged column now: it counts as 0, check() reports it, and fix=True removes the entry and recounts the totals. check() also missed an entry whose stored file name was the empty string, and entries sharing one file name beyond the first 512.
  • In a child that can't use a cache after a fork (see Changed), several calls didn't say so. Opening a FanoutCache there with size_limit, cull_limit or eviction_policy wrote those settings to the shards ahead of the one it couldn't use, for every process, and then raised; no shard is touched now. A call on a handle the child had closed reopened it, with a new connection on the file, before raising. Cache.evict(None) returned 0, and remove_old_formats() ran. Leaving the inherited with cache.transact(): block raised TransactionAborted, or returned quietly on an exception, when another thread had rolled the transaction back just before the fork. And in another library's after_in_child hook registered before diskcache_oxide was imported, stats() answered, leaving the block could wait out the timeout, and FanoutCache.clear() could empty some shards before raising. All of these raise ForkedInsideTransactionError at once now, and change nothing.
  • A Caches registry or a durable=True Django alias that had failed to open a cache in the parent kept serving that NullCache in a forked child for the rest of the parent's retry window (up to 10 seconds). A child starts without its parent's failed opens now: it opens the cache itself, or raises ForkedInsideTransactionError if it can't use it.
  • A pickle hook or a registered type's decode that called a cache its process couldn't use after a fork had its RuntimeError read as a value that can't be decoded, so the read deleted the row it was reading from a healthy cache, or answered a miss. The error passes through as itself now and the row stays.
  • check(fix=True) could remove the value file of a transaction held open for over an hour in another process, which then committed an entry with no file. It removes a file no entry names only under the write lock now, so it waits for that transaction (and raises Timeout if it outlasts the timeout).
  • With a types= registry on a handle, a value of a class registered only in the global registry was stored under the global name and read back through the handle's own registration of that name, as another class. set raises TypeError for it now.
  • Background maintenance's checkpoint read "another connection is checkpointing" as "everything is copied" and went on to reset the write-ahead log under the write lock, where the whole copy could then run and hold up writers.
  • check(fix=True) could wait about twice its timeout: the VACUUM at its end and the checkpoint after it each got the full time.