{"record":{"id":"30476d67241ba2de","repo":"greenrobot/greenDAO","slug":"interrupted-while-waiting-for-operation-to-complet","errorCode":null,"errorMessage":"Interrupted while waiting for operation to complete","messagePattern":"Interrupted while waiting for operation to complete","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/async/AsyncOperation.java","lineNumber":162,"sourceCode":"        return throwable != null;\n    }\n\n    public boolean isCompleted() {\n        return completed;\n    }\n\n    /**\n     * Waits until the operation is complete. If the thread gets interrupted, any {@link InterruptedException} will be\n     * rethrown as a {@link DaoException}.\n     *\n     * @return Result if any, see {@link #getResult()}\n     */\n    public synchronized Object waitForCompletion() {\n        while (!completed) {\n            try {\n                wait();\n            } catch (InterruptedException e) {\n                throw new DaoException(\"Interrupted while waiting for operation to complete\", e);\n            }\n        }\n        return result;\n    }\n\n    /**\n     * Waits until the operation is complete, but at most the given amount of milliseconds.If the thread gets\n     * interrupted, any {@link InterruptedException} will be rethrown as a {@link DaoException}.\n     *\n     * @return true if the operation completed in the given time frame.\n     */\n    public synchronized boolean waitForCompletion(int maxMillis) {\n        if (!completed) {\n            try {\n                wait(maxMillis);\n            } catch (InterruptedException e) {\n                throw new DaoException(\"Interrupted while waiting for operation to complete\", e);\n            }","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/async/AsyncOperation.java#L144-L180","documentation":"The blocking AsyncOperation.waitForCompletion() waits on the operation's monitor until the async executor marks it completed. If the waiting thread is interrupted while blocked in wait(), greendao converts the InterruptedException into a DaoException so the interrupted-wait cannot be silently ignored.","triggerScenarios":"Calling getResult() (or waitForCompletion()) on an AsyncOperation whose thread gets interrupted — e.g. the enclosing worker/executor thread is shut down, an Activity timeout cancels the thread, or another thread calls interrupt() on it.","commonSituations":"Blocking on async DAO results inside Android worker threads that are interrupted during app teardown, ExecutorService.shutdownNow(), or AsyncTask cancellation.","solutions":["Do not interrupt threads that are blocked on async DAO completion; check your thread pool / AsyncTask shutdown logic.","Catch DaoException and restore the interrupt flag (Thread.currentThread().interrupt()) so cancellation semantics are preserved.","Prefer the non-blocking AsyncOperationListener onOperationCompleted callback over blocking waits.","Use waitForCompletion(int maxMillis) with a timeout if indefinite blocking is undesirable."],"exampleFix":"// before\nObject result = asyncSession.insert(entity).getResult();\n// after\ntry {\n    Object result = asyncSession.insert(entity).getResult();\n} catch (DaoException e) {\n    Thread.currentThread().interrupt();\n    Log.w(TAG, \"interrupted waiting for async op\", e);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Object result = op.getResult();\n} catch (DaoException e) {\n    Thread.currentThread().interrupt(); // restore interrupt status\n    return null;\n}","preventionTips":["Avoid blocking waits on threads that get interrupted (AsyncTask, shutdownNow)","Prefer AsyncOperationListener callbacks over getResult()/waitForCompletion()","Restore the interrupt flag when catching this DaoException"],"tags":["async","interrupt","threading"],"backgroundTag":"request-timeout","analyzedSha":"0bbb338e17c4cf28aba5aae62d42f73f50186157","analyzedAt":"2026-09-08T03:22:36.050Z","contentChangedAt":"2026-09-08T03:22:36.050Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}