NativeScript/NativeScript · error · IllegalStateException

cache is closed

Error message

cache is closed

What it means

DiskLruCache operations (get, edit, remove, flush) call checkNotClosed(), which throws IllegalStateException if the cache was already closed (journalWriter == null). Once close() runs, the cache cannot be used again — create a new instance instead.

Source

Thrown at packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/image/DiskLruCache.java:672

		lruEntries.remove(key);

		if (journalRebuildRequired()) {
			executorService.submit(cleanupCallable);
		}

		return true;
	}

	/**
	 * Returns true if this cache has been closed.
	 */
	public boolean isClosed() {
		return journalWriter == null;
	}

	private void checkNotClosed() {
		if (journalWriter == null) {
			throw new IllegalStateException("cache is closed");
		}
	}

	/**
	 * Force buffered operations to the filesystem.
	 */
	public synchronized void flush() throws IOException {
		checkNotClosed();
		trimToSize();
		journalWriter.flush();
	}

	/**
	 * Closes this cache. Stored values will remain on the filesystem.
	 */
	public synchronized void close() throws IOException {
		if (journalWriter == null) {
			return; // already closed

View on GitHub (pinned to 6800aefa65)

Solutions

  1. Keep a single cache owner that closes the cache only when all consumers are done; null out references after close().
  2. Guard calls with cache.isClosed() before use, or recreate via DiskLruCache.open(...) when closed.
  3. Synchronize close() and cache accesses on the same lock to avoid racing threads.
  4. Catch IllegalStateException and reopen the cache.

Example fix

// before
imageCache.close();
imageCache.get(key); // throws "cache is closed"
// after
if (imageCache.isClosed()) {
  imageCache = DiskLruCache.open(dir, 1, 1, maxSize);
}
imageCache.get(key);
Defensive patterns

Strategy: validation

Validate before calling

if (cache.isClosed()) {
  cache = DiskLruCache.open(cacheDir, 1, 1, maxSize);
}

Try / catch

try {
  return cache.get(key);
} catch (IllegalStateException closed) {
  cache = DiskLruCache.open(cacheDir, 1, 1, maxSize);
  return cache.get(key);
}

Prevention

When it happens

Trigger: Calling get/edit/remove/flush on a DiskLruCache instance after close() has been called, typically from a stale singleton reference or a background thread racing with close()/delete().

Common situations: Activity/fragment lifecycle closing the image cache while a background load still uses it; calling delete() (which closes) then continuing to use the old reference; double-close followed by use.

Related errors


AI-assisted analysis of NativeScript/NativeScript@6800aefa65 (2026-08-30). Data as JSON: /api/errors/12a4378bec562be8. Report an issue: GitHub.