Fixed potential crash when computer stops immediately while still initializing disk from image.

This commit is contained in:
Florian Nücke
2022-01-09 09:13:05 +01:00
parent 7797c068fa
commit 1e59043b01
2 changed files with 40 additions and 18 deletions

View File

@@ -89,19 +89,11 @@ public abstract class AbstractBlockDeviceVMDevice<TBlock extends BlockDevice, TI
@Override
public void suspend() {
if (device != null) {
try {
device.close();
} catch (final IOException e) {
LOGGER.error(e);
}
}
closeBlockDevice();
if (blobHandle != null) {
BlobStorage.close(blobHandle);
}
device = null;
}
@Override
@@ -163,6 +155,20 @@ public abstract class AbstractBlockDeviceVMDevice<TBlock extends BlockDevice, TI
protected abstract TBlock createBlockDevice() throws IOException;
protected void closeBlockDevice() {
if (device == null) {
return;
}
try {
device.close();
} catch (final IOException e) {
LOGGER.error(e);
}
device = null;
}
protected void handleDataAccess() {
}

View File

@@ -40,15 +40,7 @@ public final class HardDriveVMDeviceWithInitialData extends HardDriveVMDevice {
@Subscribe
public void handleResumedRunningEvent(final VMResumedRunningEvent event) {
if (copyJob != null) {
try {
copyJob.get();
} catch (final Throwable e) {
LOGGER.error(e);
} finally {
copyJob = null;
}
}
joinCopyJob();
}
///////////////////////////////////////////////////////////////////
@@ -71,4 +63,28 @@ public final class HardDriveVMDeviceWithInitialData extends HardDriveVMDevice {
}
return device;
}
@Override
protected void closeBlockDevice() {
// Join the copy job before releasing the device to avoid writes from thread to closed device.
// Since we use memory mapped memory, closing the device leads to it holding a dead pointer, meaning
// further access to it will hard-crash the JVM.
joinCopyJob();
super.closeBlockDevice();
}
///////////////////////////////////////////////////////////////////
private void joinCopyJob() {
if (copyJob != null) {
try {
copyJob.get();
} catch (final Throwable e) {
LOGGER.error(e);
} finally {
copyJob = null;
}
}
}
}