{"record":{"id":"f3ac504d476e5fd8","repo":"MuntashirAkon/AppManager","slug":"error-msg-constant-invalid-unknown-file-handle-for-calling","errorCode":null,"errorMessage":"ERROR_MSG (constant; invalid/unknown file handle for calling pid)","messagePattern":"ERROR_MSG \\(constant; invalid/unknown file handle for calling pid\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/FileContainer.java","lineNumber":26,"sourceCode":"import androidx.annotation.NonNull;\n\nimport java.io.IOException;\n\n// Copyright 2022 John \"topjohnwu\" Wu\nclass FileContainer {\n\n    private final static String ERROR_MSG = \"Requested file was not opened!\";\n\n    private int nextHandle = 0;\n    // pid -> handle -> holder\n    private final SparseArray<SparseArray<OpenFile>> files = new SparseArray<>();\n\n    @NonNull\n    synchronized OpenFile get(int handle) throws IOException {\n        int pid = Binder.getCallingPid();\n        SparseArray<OpenFile> pidFiles = files.get(pid);\n        if (pidFiles == null)\n            throw new IOException(ERROR_MSG);\n        OpenFile h = pidFiles.get(handle);\n        if (h == null)\n            throw new IOException(ERROR_MSG);\n        return h;\n    }\n\n    synchronized int put(OpenFile h) {\n        int pid = Binder.getCallingPid();\n        SparseArray<OpenFile> pidFiles = files.get(pid);\n        if (pidFiles == null) {\n            pidFiles = new SparseArray<>();\n            files.put(pid, pidFiles);\n        }\n        int handle = nextHandle++;\n        pidFiles.append(handle, h);\n        return handle;\n    }\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/FileContainer.java#L8-L44","documentation":"FileContainer.get(handle) looks up an OpenFile by handle for the calling PID. It throws IOException(\"Requested file was not opened!\") when there is no handle table at all for the calling process (files.get(pid) == null), meaning this process never opened any file through this container.","triggerScenarios":"Calling get(handle) from a process (Binder.getCallingPid()) that never registered any OpenFile via put() — e.g. a remote process requesting a handle it does not own, or calls made after the client process's table was cleared.","commonSituations":"IPC clients reusing a handle from a different process; the client process died and pidDied() purged its handles, then it retries; server restart losing in-memory handle tables.","solutions":["Ensure put() is called (via the FileSystemManager open API) in the same process before calling get().","Re-open the file to obtain a fresh handle after a process restart or crash.","Verify handles are not shared across processes; handles are per-PID and not transferable."],"exampleFix":"// before\nOpenFile f = container.get(handle); // throws if pid has no table\n// after\ntry {\n    OpenFile f = container.get(handle);\n} catch (IOException e) {\n    handle = container.put(reopenFile());\n}","handlingStrategy":"try-catch","validationCode":"boolean known = lastPutPid == android.os.Process.myPid() && lastPutHandle >= 0;","typeGuard":null,"tryCatchPattern":"try {\n    OpenFile f = container.get(handle);\n} catch (IOException e) {\n    if (\"Requested file was not opened!\".equals(e.getMessage())) {\n        handle = container.put(openFresh());\n    }\n}","preventionTips":["Only use handles returned by put() in the same process","Re-open after process restarts","Never cache handles across binder client restarts"],"tags":["ipc","handle","android"],"backgroundTag":"resource-not-found","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}