{"record":{"id":"1ca24c91552e8ea1","repo":"SonarSource/sonarqube","slug":"servlet-filter-is-already-instantiated","errorCode":null,"errorMessage":"Servlet filter  is already instantiated","messagePattern":"Servlet filter  is already instantiated","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-webserver/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java","lineNumber":61,"sourceCode":"import org.sonar.api.web.HttpFilter;\nimport org.sonar.server.http.JakartaHttpRequest;\nimport org.sonar.server.http.JakartaHttpResponse;\nimport org.sonar.server.platform.PlatformImpl;\n\n/**\n * Inspired by http://stackoverflow.com/a/7592883/229031\n */\npublic final class MasterServletFilter implements Filter {\n\n  private static final String SCIM_FILTER_PATH = \"/api/scim/v2/\";\n  private static final Logger LOG = LoggerFactory.getLogger(MasterServletFilter.class);\n  private static volatile MasterServletFilter instance;\n\n  private HttpFilter[] httpFilters;\n\n  public MasterServletFilter() {\n    if (instance != null) {\n      throw new IllegalStateException(\"Servlet filter \" + getClass().getName() + \" is already instantiated\");\n    }\n    instance = this;\n  }\n\n  @Override\n  public void init(FilterConfig config) {\n    // Filters are already available in the container unless a database migration is required. See\n    // org.sonar.server.startup.RegisterServletFilters.\n    List<HttpFilter> httpFilterList = PlatformImpl.getInstance().getContainer().getComponentsByType(HttpFilter.class);\n    init(httpFilterList);\n  }\n\n  @CheckForNull\n  public static MasterServletFilter getInstance() {\n    return instance;\n  }\n\n  @VisibleForTesting","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java#L43-L79","documentation":"MasterServletFilter is a servlet filter designed as a singleton: its constructor stores itself in a static volatile 'instance' field and throws IllegalStateException if an instance already exists. This protects against Tomcat (or a test harness) instantiating the filter twice.","triggerScenarios":"Registering MasterServletFilter more than once in web.xml or programmatically, or restarting/redeploying the webapp within the same JVM classloader lifetime so a second filter instance is constructed while the static field still holds the first.","commonSituations":"Duplicate <filter> mappings for the same class in web.xml; hot redeploy without classloader refresh; tests constructing the filter manually after the server already created one.","solutions":["Ensure MasterServletFilter is declared/registered exactly once in the web application","If redeploying, ensure the old webapp classloader is discarded so the static instance is released (full restart if needed)","In tests, reset the static instance (set to null via reflection/test hook) before constructing a new instance"],"exampleFix":"// before\n<filter-class>...MasterServletFilter</filter-class>\n... duplicate second <filter> with same class ...\n// after\nsingle <filter> declaration mapped once in web.xml","handlingStrategy":"type-guard","validationCode":"if (MasterServletFilter.class.getDeclaredField(\"instance\").get(null) != null) throw new IllegalStateException(\"MasterServletFilter already instantiated; do not register twice\");","typeGuard":"boolean notYetInstantiated() throws Exception { return MasterServletFilter.class.getDeclaredField(\"instance\").get(null) == null; }","tryCatchPattern":"try { new MasterServletFilter(); } catch (IllegalStateException e) { log.warn(\"Filter already active in this JVM; reusing existing instance\"); }","preventionTips":["Register the filter exactly once (single mapping in web.xml)","Perform full server restarts instead of partial hot redeploys","In tests, reset the static instance between tests"],"tags":["servlet","singleton","initialization"],"backgroundTag":"module-init-failed","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}