{"record":{"id":"85dae8947a878577","repo":"apache/dubbo","slug":"scopebeanfactory-is-destroyed","errorCode":null,"errorMessage":"ScopeBeanFactory is destroyed","messagePattern":"ScopeBeanFactory is destroyed","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java","lineNumber":387,"sourceCode":"                                \"An error occurred when destroy bean [name=\" + beanInfo.name + \", bean=\"\n                                        + beanInfo.instance + \"]: \" + e,\n                                e);\n                    }\n                }\n            }\n            registeredBeanInfos.clear();\n            registeredBeanDefinitions.clear();\n            beanCache.clear();\n        }\n    }\n\n    public boolean isDestroyed() {\n        return destroyed.get();\n    }\n\n    private void checkDestroyed() {\n        if (destroyed.get()) {\n            throw new IllegalStateException(\"ScopeBeanFactory is destroyed\");\n        }\n    }\n\n    static final class BeanInfo {\n        private final String name;\n        private final Object instance;\n\n        BeanInfo(String name, Object instance) {\n            this.name = name;\n            this.instance = instance;\n        }\n    }\n\n    static final class BeanDefinition<T> {\n\n        private final String name;\n        private final Class<T> beanClass;\n        private final Supplier<T> beanFactory;","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java#L369-L405","documentation":"Thrown by ScopeBeanFactory.checkDestroyed whenever any mutating operation (registerBean, createAndRegisterBean, etc.) is attempted after destroy() has flipped the destroyed flag. Once a scope is destroyed its maps are cleared and further registration is illegal; Dubbo fails fast rather than registering into a dead scope.","triggerScenarios":"Calling registerBean / registerBeanFactory / registerBeanDefinition / getOrRegisterBean after ScopeBeanFactory.destroy() has run (typically during ApplicationModel/FrameworkModel shutdown). Also when a shutdown hook or scope-destroy races with late bean registration.","commonSituations":"Trying to register a bean during or after application shutdown; reusing a ScopeModel that was already destroyed; a delayed/async component attempting registration after context close; tests that tear down the model and then touch the factory.","solutions":["Guard every registration with if (!factory.isDestroyed()) { ... } when registration can race with shutdown.","Ensure the ScopeModel/ApplicationModel is still alive before performing registration (check isDestroyed()).","Cancel/await async tasks that may register beans before destroying the model, or destroy after they complete.","In tests, do not reuse a destroyed model; create a fresh ApplicationModel for each test."],"exampleFix":"// before\napplicationModel.destroy();\nfactory.registerBean(\"late\", LateBean.class);  // throws\n// after\nif (!factory.isDestroyed()) {\n    factory.registerBean(\"late\", LateBean.class);\n} else {\n    log.warn(\"Skipped late registration: scope already destroyed\");\n}","handlingStrategy":"validation","validationCode":"// Guard registration against a destroyed scope\nboolean safeRegister(ScopeBeanFactory f, String name, Class<?> type) {\n    if (f.isDestroyed()) {\n        log.warn(\"ScopeBeanFactory destroyed; skipping registration of {}\", name);\n        return false;\n    }\n    f.registerBean(name, type);\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n    f.registerBean(name, type);\n} catch (IllegalStateException e) {\n    if (\"ScopeBeanFactory is destroyed\".equals(e.getMessage())) {\n        // scope already torn down; skip or re-queue the registration\n    } else throw e;\n}","preventionTips":["Check isDestroyed() before any registration that can race with shutdown.","Ensure async tasks that register beans are stopped before destroying the ScopeModel.","Never reuse a destroyed ApplicationModel/FrameworkModel; create a fresh one.","In tests, tear down and rebuild the model per test rather than reusing."],"tags":["bean-factory","lifecycle","shutdown","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}