{"record":{"id":"f8caa9e8b17bd4f1","repo":"greenrobot/greenDAO","slug":"interrupted-while-waiting-for-all-operations-to-co","errorCode":null,"errorMessage":"Interrupted while waiting for all operations to complete","messagePattern":"Interrupted while waiting for all operations to complete","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/async/AsyncOperationExecutor.java","lineNumber":114,"sourceCode":"\n    public void setListenerMainThread(AsyncOperationListener listenerMainThread) {\n        this.listenerMainThread = listenerMainThread;\n    }\n\n    public synchronized boolean isCompleted() {\n        return countOperationsEnqueued == countOperationsCompleted;\n    }\n\n    /**\n     * Waits until all enqueued operations are complete. If the thread gets interrupted, any\n     * {@link InterruptedException} will be rethrown as a {@link DaoException}.\n     */\n    public synchronized void waitForCompletion() {\n        while (!isCompleted()) {\n            try {\n                wait();\n            } catch (InterruptedException e) {\n                throw new DaoException(\"Interrupted while waiting for all operations to complete\", e);\n            }\n        }\n    }\n\n    /**\n     * Waits until all enqueued operations are complete, but at most the given amount of milliseconds. If the thread\n     * gets interrupted, any {@link InterruptedException} will be rethrown as a {@link DaoException}.\n     *\n     * @return true if operations completed in the given time frame.\n     */\n    public synchronized boolean waitForCompletion(int maxMillis) {\n        if (!isCompleted()) {\n            try {\n                wait(maxMillis);\n            } catch (InterruptedException e) {\n                throw new DaoException(\"Interrupted while waiting for all operations to complete\", e);\n            }\n        }","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/async/AsyncOperationExecutor.java#L96-L132","documentation":"AsyncOperationExecutor.waitForCompletion() blocks until all enqueued operations have finished (the executor's count reaches the expected total). If the calling thread is interrupted while waiting on the executor monitor, the InterruptedException is converted into this DaoException.","triggerScenarios":"Calling asyncSession.waitForCompletion() (executor-level, waits for ALL pending async ops) from a thread that is interrupted — e.g. during ExecutorService shutdownNow, Activity destruction with thread cancellation, or explicit interrupt().","commonSituations":"App teardown or test frameworks interrupting threads that were waiting for a batch of async DAO writes to drain before proceeding.","solutions":["Don't interrupt the waiting thread; drain async ops before shutdown, or call waitForCompletion earlier in lifecycle.","Catch DaoException and restore the interrupt flag (Thread.currentThread().interrupt()).","Use waitForCompletion(int maxMillis) if you want a bounded wait that returns false on timeout instead of blocking forever.","Prefer listener callbacks (AsyncOperationListener.onOperationCompleted) to avoid blocking the main/work thread."],"exampleFix":"// before\nasyncSession.waitForCompletion(); // throws DaoException when interrupted\n// after\ntry {\n    asyncSession.waitForCompletion();\n} catch (DaoException e) {\n    Thread.currentThread().interrupt();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    asyncSession.waitForCompletion();\n} catch (DaoException e) {\n    Thread.currentThread().interrupt();\n    // abort batch or persist progress, then return\n}","preventionTips":["Drain pending async operations before shutting down executor threads","Prefer waitForCompletion(maxMillis) to avoid indefinite blocking","Use executor-level listeners instead of blocking waits where possible"],"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"}