Skip to content

Commit

Permalink
[core] IJPL-174780: provide close stack trace in 'already closed' exc…
Browse files Browse the repository at this point in the history
…eption in MMappedFileStorage

For better diagnostics of 'storage already closed' scenarios

GitOrigin-RevId: 086bf899a53625f7d3424158c8438f8cdf42d017
  • Loading branch information
Ruslan Cheremin authored and intellij-monorepo-bot committed Jan 8, 2025
1 parent c866044 commit d47c71c
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.platform.util.io.storages.mmapped;

import com.intellij.openapi.diagnostic.Logger;
Expand Down Expand Up @@ -135,6 +135,13 @@ public final class MMappedFileStorage implements Closeable, Unmappable, Cleanabl

private final RegionAllocationAtomicityLock regionAllocationAtomicityLock;

/**
* Stack trace of {@linkplain #close()} call -- stored to provide more information to 'already closed' exception
* in use-after-close scenario
*/
//@GuardedBy(pagesLock)
private transient Exception closeStackTrace = null;

/** Use {@link MMappedFileStorageFactory} */
MMappedFileStorage(@NotNull Path path,
int pageSize,
Expand Down Expand Up @@ -381,6 +388,8 @@ private void close(boolean unmap) throws IOException {
Arrays.fill(pages, null);
}
}

this.closeStackTrace = new Exception("Close stack trace");
}
}
finally {
Expand Down Expand Up @@ -411,7 +420,11 @@ private void close(boolean unmap) throws IOException {
private Page pageByIndexLocked(int pageIndex) throws IOException {
synchronized (pagesLock) {
if (!channel.isOpen()) {
throw new ClosedStorageException("Storage already closed");
ClosedStorageException ex = new ClosedStorageException("Storage already closed");
if (closeStackTrace != null) {
ex.addSuppressed(closeStackTrace);
}
throw ex;
}
if (pageIndex >= pages.length) {
pages = Arrays.copyOf(pages, pageIndex + 1);
Expand Down

0 comments on commit d47c71c

Please sign in to comment.