{"record":{"id":"d0df28a817d5bf5c","repo":"stanfordnlp/CoreNLP","slug":"cannot-modify-redwood-when-within-a-track","errorCode":null,"errorMessage":"Cannot modify Redwood when within a track","messagePattern":"Cannot modify Redwood when within a track","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/logging/Redwood.java","lineNumber":649,"sourceCode":"      head = null;\n    }\n\n    public RecordHandlerTree(LogRecordHandler head) {\n      this.isRoot = false;\n      this.head = head;\n    }\n\n    // -- Core Tree Methods --\n    public LogRecordHandler head(){\n      return head;\n    }\n    public Iterator<RecordHandlerTree> children(){\n      return children.iterator();\n    }\n    // -- Utility Methods --\n    public void addChild(LogRecordHandler handler){\n      if(Redwood.depth != 0){\n        throw new IllegalStateException(\"Cannot modify Redwood when within a track\");\n      }\n      children.add(new RecordHandlerTree(handler));\n    }\n    protected void addChildTree(RecordHandlerTree tree){\n      if(Redwood.depth != 0){\n        throw new IllegalStateException(\"Cannot modify Redwood when within a track\");\n      }\n      children.add(tree);\n    }\n    public LogRecordHandler removeChild(LogRecordHandler handler){\n      if(Redwood.depth != 0){\n        throw new IllegalStateException(\"Cannot modify Redwood when within a track\");\n      }\n      Iterator<RecordHandlerTree> iter = children();\n      while(iter.hasNext()){\n        LogRecordHandler cand = iter.next().head();\n        if(cand == handler){\n          iter.remove();","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/logging/Redwood.java#L631-L667","documentation":"Redwood.RecordHandlerTree.addChild(handler) throws IllegalStateException when Redwood.depth != 0, i.e. when a logging track is currently open. The handler tree — the configuration of which log handlers are active — may only be modified at track depth 0, because mutating it mid-track would corrupt the active track stack.","triggerScenarios":"Calling Redwood.addChild(handler) (or convenience setters Redwood.output/stdout/stderr/slf4j/javaUtil/apply that delegate to it) between startTrack() and endTrack(), e.g. inside a logged region of code.","commonSituations":"Library code that reconfigures logging inside a tracked training loop; user code calling RedwoodConfiguration.apply(...) inside a track; handlers added per-request while a top-level track is open.","solutions":["Move all Redwood.addChild/output/apply configuration calls to program startup, before any startTrack()","Defer configuration until after the tracked region ends (Redwood.depth == 0)","If dynamic handler changes are needed, end and restart the track around the reconfiguration","Assert Redwood.depth == 0 (or catch IllegalStateException) in helper code that configures logging"],"exampleFix":"// before\nRedwood.startTrack(\"run\");\nRedwood.show(Redwood.ERR); // reconfigures handlers -> throws\n// after\nRedwood.show(Redwood.ERR);\nRedwood.startTrack(\"run\");\n// ... work ...\nRedwood.endTrack(\"run\");","handlingStrategy":"validation","validationCode":"if (Redwood.depth != 0) {\n  throw new IllegalStateException(\"Configure Redwood handlers only outside tracks\");\n}\nRedwood.addChild(handler);","typeGuard":null,"tryCatchPattern":"try {\n  Redwood.addChild(handler);\n} catch (IllegalStateException e) {\n  log.warn(\"Deferred handler add: {}\", e.getMessage());\n  pendingHandlers.add(handler); // apply after endTrack\n}","preventionTips":["Configure all handlers at startup, before any startTrack()","Never call RedwoodConfiguration.apply/output/stdout/stderr inside logged regions","Queue desired handler changes and apply them at depth 0","Add assertions in shared utility code that wraps Redwood configuration"],"tags":["logging","redwood","configuration","state"],"backgroundTag":"invalid-state-transition","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}