{"record":{"id":"e9d0c4ecd117c5b9","repo":"apache/beam","slug":"dofn-r-has-multiple-timerspecs-with-the-same-name-s","errorCode":null,"errorMessage":"DoFn %r has multiple TimerSpecs with the same name: %s.","messagePattern":"DoFn %r has multiple TimerSpecs with the same name: (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/userstate.py","lineNumber":306,"sourceCode":"\n  # A Stateful DoFn is a DoFn that uses user state or timers.\n  all_state_specs, all_timer_specs = get_dofn_specs(dofn)\n  return bool(all_state_specs or all_timer_specs)\n\n\ndef validate_stateful_dofn(dofn: 'DoFn') -> None:\n  \"\"\"Validates the proper specification of a stateful DoFn.\"\"\"\n\n  # Get state and timer specs.\n  all_state_specs, all_timer_specs = get_dofn_specs(dofn)\n\n  # Reject DoFns that have multiple state or timer specs with the same name.\n  if len(all_state_specs) != len(set(s.name for s in all_state_specs)):\n    raise ValueError(\n        'DoFn %r has multiple StateSpecs with the same name: %s.' %\n        (dofn, all_state_specs))\n  if len(all_timer_specs) != len(set(s.name for s in all_timer_specs)):\n    raise ValueError(\n        'DoFn %r has multiple TimerSpecs with the same name: %s.' %\n        (dofn, all_timer_specs))\n\n  # Reject DoFns that use timer specs without corresponding timer callbacks.\n  for timer_spec in all_timer_specs:\n    if not timer_spec._attached_callback:\n      raise ValueError((\n          'DoFn %r has a TimerSpec without an associated on_timer '\n          'callback: %s.') % (dofn, timer_spec))\n    method_name = timer_spec._attached_callback.__name__\n    if (timer_spec._attached_callback != getattr(dofn, method_name,\n                                                 None).__func__):  # type: ignore[union-attr]\n      raise ValueError((\n          'The on_timer callback for %s is not the specified .%s method '\n          'for DoFn %r (perhaps it was overwritten?).') %\n                       (timer_spec, method_name, dofn))\n\n","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/userstate.py#L288-L324","documentation":"Apache Beam raises this ValueError during stateful DoFn validation when two or more TimerSpecs on a DoFn share the same timer name. Timer names must be unique per DoFn so the runner can map a firing timer back to exactly one spec. validate_stateful_dofn detects the collision by comparing the spec count against the set of unique timer names.","triggerScenarios":"A DoFn defines two TimerSpec attributes whose TimerSpec('name') strings are identical (e.g. two TimerSpec('expiry') with different on_timer callbacks), and validate_stateful_dofn is called at pipeline construction.","commonSituations":"Copy-pasting a timer spec and only changing the callback but not the name; renaming a callback while leaving the old spec in place; defining the same logical timer twice across refactors.","solutions":["Rename one of the duplicate TimerSpecs so timer names are unique on the DoFn.","Remove the stale/duplicate TimerSpec attribute if it is unused.","List current timers with get_dofn_specs(dofn) and audit the timer names."],"exampleFix":"// before\nclass MyDoFn(DoFn):\n  EARLY_TIMER = TimerSpec('fire', on_timer=process_early)\n  LATE_TIMER = TimerSpec('fire', on_timer=process_late)  # duplicate name\n// after\nclass MyDoFn(DoFn):\n  EARLY_TIMER = TimerSpec('early_fire', on_timer=process_early)\n  LATE_TIMER = TimerSpec('late_fire', on_timer=process_late)","handlingStrategy":"validation","validationCode":"from apache_beam.transforms.userstate import get_dofn_specs\nnames = [t.name for t in get_dofn_specs(MyDoFn)[1]]\nassert len(names) == len(set(names)), f'duplicate timer names: {names}'","typeGuard":null,"tryCatchPattern":"try:\n  validate_stateful_dofn(MyDoFn)\nexcept ValueError as e:\n  if 'multiple TimerSpecs with the same name' in str(e):\n    fix_duplicate_timer_names(MyDoFn)\n  else:\n    raise","preventionTips":["Name timers after their purpose (early_fire, expiry) to avoid accidental collisions.","Delete stale TimerSpecs instead of leaving them alongside new ones.","Call get_dofn_specs in tests to assert timer-name uniqueness."],"tags":["python","apache-beam","timers","duplicate-name","validation"],"backgroundTag":"invalid-identifier-format","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}