{"record":{"id":"f1a5a482f26a706a","repo":"apache/beam","slug":"cannot-access-state-in-non-window-observing-context","errorCode":null,"errorMessage":"Cannot access state in non-window observing context.","messagePattern":"Cannot access state in non-window observing context\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/harness/src/main/java/org/apache/beam/fn/harness/FnApiDoFnRunner.java","lineNumber":1984,"sourceCode":"      throw new UnsupportedOperationException(\n          \"Cannot access window in non-window observing context.\");\n    }\n\n    @Override\n    public Object sideInput(String tagId) {\n      throw new UnsupportedOperationException(\n          \"Cannot access sideInput in non-window observing context.\");\n    }\n\n    @Override\n    public <T> T sideInput(PCollectionView<T> view) {\n      throw new UnsupportedOperationException(\n          \"Cannot access sideInput in non-window observing context.\");\n    }\n\n    @Override\n    public State state(String stateId, boolean alwaysFetched) {\n      throw new UnsupportedOperationException(\n          \"Cannot access state in non-window observing context.\");\n    }\n\n    @Override\n    public org.apache.beam.sdk.state.Timer timer(String timerId) {\n      throw new UnsupportedOperationException(\n          \"Cannot access timer in non-window observing context.\");\n    }\n\n    @Override\n    public TimerMap timerFamily(String timerFamilyId) {\n      throw new UnsupportedOperationException(\n          \"Cannot access timerFamily in non-window observing context.\");\n    }\n  }\n\n  /** Base implementation that does not override methods which need to be window aware. */\n  private abstract class ProcessBundleContextBase extends DoFn<InputT, OutputT>.ProcessContext","sourceCodeStart":1966,"sourceCodeEnd":2002,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/FnApiDoFnRunner.java#L1966-L2002","documentation":"NonWindowObservingProcessBundleContextBase.state(String, boolean) throws UnsupportedOperationException because state access (StateCell/ValueState etc.) is keyed by window, and this context serves a method that is not window-observing. Without a bound window the runner cannot address the state cell.","triggerScenarios":"Calling c.state(stateId, ...) from a @ProcessElement method without a BoundedWindow parameter on a keyed DoFn, or from @StartBundle/@FinishBundle.","commonSituations":"Adding stateful logic (dedup caches, counters) to an existing DoFn without updating the method signature; forgetting that state APIs also require @ProcessElement context, not bundle lifecycle context; running a stateful DoFn where the runner picked the non-window-observing invoker.","solutions":["Add a BoundedWindow parameter to the @ProcessElement method so state access is permitted via the window-observing context.","Only call state() from @ProcessElement on a keyed PCollection (KV input) — move any bundle-level logic to ordinary local fields.","Verify the state id is declared via @StateId on the DoFn; undeclared ids will also fail at validation time."],"exampleFix":"// before\n@ProcessElement\npublic void processElement(ProcessContext c) {\n  ValueState<Integer> s = c.state(seenId, ...); // throws\n}\n\n// after\n@ProcessElement\npublic void processElement(ProcessContext c, BoundedWindow window) {\n  ValueState<Integer> s = c.state(seenId);\n}","handlingStrategy":"validation","validationCode":"// State access requires a window-observing, keyed @ProcessElement:\nboolean stateReady = Arrays.stream(dofn.getClass().getDeclaredMethods())\n    .filter(m -> m.isAnnotationPresent(ProcessElement.class))\n    .allMatch(m -> Arrays.asList(m.getParameterTypes()).contains(BoundedWindow.class));\n// plus: input must be PCollection<KV<K, V>> (keyed).","typeGuard":"boolean canUseState(DoFn<?> doFn) {\n  return Arrays.stream(doFn.getClass().getDeclaredMethods())\n      .filter(m -> m.isAnnotationPresent(ProcessElement.class))\n      .anyMatch(m -> Arrays.asList(m.getParameterTypes()).contains(BoundedWindow.class));\n}","tryCatchPattern":"try {\n  ValueState<Integer> s = c.state(seenId);\n} catch (UnsupportedOperationException e) {\n  throw new IllegalStateException(\"state() needs a window-observing @ProcessElement on a keyed PCollection\", e);\n}","preventionTips":["Use state only with KV inputs and a BoundedWindow-parameterized @ProcessElement.","Declare all states with @StateId and reference them via fields, never ad-hoc ids.","Do not touch state in bundle lifecycle methods; use local variables instead."],"tags":["java","beam","apache-beam","state","dofn"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}