Fixed #535: Crash on closing cached ZipFileSystem for deleted files

This commit is contained in:
Akash Yadav
2022-10-15 16:27:55 +05:30
parent 9c8d44283b
commit 2f872ebfb5
4 changed files with 38 additions and 33 deletions
@@ -1013,7 +1013,7 @@ public class EditorActivity extends IDEActivity
try {
unregisterReceiver(mLogReceiver);
} catch (Throwable th) {
LOG.error("Failed to unregister LogReceiver", th);
LOG.error("Failed to release resources", th);
}
unbindService(mGradleServiceConnection);
super.onDestroy();
@@ -1475,12 +1475,14 @@ public class EditorActivity extends IDEActivity
}
private void stopServices() {
if (IDELanguageClientImpl.isInitialized()) {
IDELanguageClientImpl.shutdown();
try {
if (IDELanguageClientImpl.isInitialized()) {
IDELanguageClientImpl.shutdown();
}
shutdownLanguageServers();
} catch (Throwable err) {
LOG.error("Unable to stop editor services. Please report this issue.", err);
}
shutdownLanguageServers();
}
private void shutdownLanguageServers() {
@@ -20,6 +20,7 @@ package com.itsaky.androidide.javac.services.fs
import com.itsaky.androidide.zipfs2.ZipFileSystem
import com.itsaky.androidide.zipfs2.ZipFileSystemProvider
import com.sun.tools.javac.file.RelativePath.RelativeDirectory
import java.io.IOException
import java.nio.file.Path
import javax.lang.model.SourceVersion
@@ -33,7 +34,7 @@ class CachedJarFileSystem(
zfpath: Path?,
env: MutableMap<String, *>?
) : ZipFileSystem(provider, zfpath, env) {
internal val packages = mutableMapOf<RelativeDirectory, Path>()
override fun close() {
@@ -41,6 +42,7 @@ class CachedJarFileSystem(
// This is called manually by the Java LSP
}
@Throws(IOException::class)
fun doClose() {
super.close()
}
@@ -17,6 +17,7 @@
package com.itsaky.androidide.javac.services.fs
import com.itsaky.androidide.utils.ILogger
import com.itsaky.androidide.zipfs2.JarFileSystemProvider
import com.itsaky.androidide.zipfs2.ZipFileSystem
import java.nio.file.FileSystem
@@ -32,7 +33,8 @@ import kotlin.io.path.pathString
*/
object CachingJarFileSystemProvider : JarFileSystemProvider() {
private val cachedFs = ConcurrentHashMap<String, CachedJarFileSystem>()
private val log = ILogger.newInstance("CachingJarFileSystemProvider")
override fun createFs(path: Path, env: MutableMap<String, *>?): ZipFileSystem {
val cached = cachedFs[path.normalize().pathString]
if (cached != null) {
@@ -46,7 +48,13 @@ object CachingJarFileSystemProvider : JarFileSystemProvider() {
}
fun clearCache() {
cachedFs.values.forEach { it.doClose() }
cachedFs.values.forEach {
try {
it.doClose()
} catch (err: Throwable) {
log.error("Failed to close cached zip file system: $it", err)
}
}
cachedFs.clear()
}
@@ -144,13 +144,7 @@ public abstract class BaseApplication extends MultiDexApplication {
new File(FileUtil.getExternalStorageDir(), "idelog.txt").getAbsolutePath(),
ThrowableUtils.getFullStackTrace(th));
}
@NonNull
@Contract(" -> new")
public final File getLogSenderDir() {
return new File(getRootDir(), "logsender");
}
public final File getTempProjectDir() {
return Environment.mkdirIfNotExits(new File(Environment.TMP_DIR, "tempProject"));
}
@@ -158,11 +152,7 @@ public abstract class BaseApplication extends MultiDexApplication {
public PreferenceManager getPrefManager() {
return mPrefsManager;
}
public File[] listProjects() {
return getProjectsDir().listFiles(File::isDirectory);
}
public File getProjectsDir() {
return Environment.PROJECTS_DIR;
}
@@ -195,28 +185,31 @@ public abstract class BaseApplication extends MultiDexApplication {
openUrl("mailto:" + EMAIL);
}
public void openUrl(String url) {
try {
Intent open = new Intent();
open.setAction(Intent.ACTION_VIEW);
open.setData(Uri.parse(url));
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(open);
} catch (Throwable th) {
ToastUtilsKt.toast(th.getMessage(), Toaster.Type.ERROR);
}
openUrl(url, null);
}
public void openTelegram(String url) {
openUrl(url, "org.telegram.messenger");
}
public void openUrl(String url, String pkg) {
try {
Intent open = new Intent();
open.setAction(Intent.ACTION_VIEW);
open.setData(Uri.parse(url));
open.setPackage("org.telegram.messenger");
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (pkg != null) {
open.setPackage(pkg);
}
startActivity(open);
} catch (Throwable th1) {
openUrl(url);
} catch (Throwable th) {
if (pkg != null) {
openUrl(url);
} else {
ToastUtilsKt.toast(th.getMessage(), Toaster.Type.ERROR);
}
}
}
}