Fix a race condition when loading non-mmapd byte windows
commita720a0e77dd05f4b9f93e2ff07e66777c0ed1f31
authorShawn O. Pearce <spearce@spearce.org>
Sun, 8 Mar 2009 00:17:15 +0000 (7 16:17 -0800)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Mon, 9 Mar 2009 20:16:35 +0000 (9 21:16 +0100)
tree47eb16849e80fbe9a0a9bf8668fca39cc6aced79
parent9c26a41ec0914b009bd96253fa387b73d271011e
Fix a race condition when loading non-mmapd byte windows

Back in 439d441b0800 ("Change ByteArrayWindow to read content
outside of WindowCache's lock") I introduced a race condition
to WindowCache.

It is possible for a ByteArrayWindow to be allocated and put
into the cache, and then for the calling thread to get put
to sleep before it can actually load the window from disk.
By the time the thread wakes up again, the window may have
already been evicted from the cache, and its underlying file
was closed.  This results in stack traces such as:

java.lang.RuntimeException: Cannot fault in window
        at org.spearce.jgit.lib.ByteArrayWindow.ensureLoaded(ByteArrayWindow.java:111)
...
Caused by: java.nio.channels.ClosedChannelException
        at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:91)

Now when we allocate a ByteArrayWindow for a specific window reference
we increment the file reference count one additional time, forcing the
file to stay open even if the window was evicted from the cache.  The
reference count is decremented once the window loads successfully, and
this puts us back into the state of 1 reference count value for each
window still in the window cache.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Fix a race condition when loading non-mmapd byte windows

"Shawn O. Pearce" <spearce@spearce.org> wrote:
>  Oh hell, I missed the fact that markLoaded(ByteWindow) may be
>  dropping the last reference and need to close the file.

*and* you need to squash this in to prevent a deadlock:

Frell.  What a Saturday.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
org.spearce.jgit/src/org/spearce/jgit/lib/ByteArrayWindow.java
org.spearce.jgit/src/org/spearce/jgit/lib/ByteBufferWindow.java
org.spearce.jgit/src/org/spearce/jgit/lib/ByteWindow.java
org.spearce.jgit/src/org/spearce/jgit/lib/WindowCache.java
org.spearce.jgit/src/org/spearce/jgit/lib/WindowedFile.java