{"record":{"id":"e5d15e22f7574998","repo":"grafana/k6","slug":"dispatching-key-event-down-w","errorCode":null,"errorMessage":"dispatching key event down: %w","messagePattern":"dispatching key event down: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/keyboard.go","lineNumber":133,"sourceCode":"\tk.pressedKeysMu.Unlock()\n\n\tkeyType := input.KeyDown\n\tif text == \"\" {\n\t\tkeyType = input.KeyRawDown\n\t}\n\n\taction := input.DispatchKeyEvent(keyType).\n\t\tWithModifiers(input.Modifier(k.modifiers)).\n\t\tWithKey(keyDef.Key).\n\t\tWithWindowsVirtualKeyCode(keyDef.KeyCode).\n\t\tWithCode(keyDef.Code).\n\t\tWithLocation(keyDef.Location).\n\t\tWithIsKeypad(keyDef.Location == 3).\n\t\tWithText(text).\n\t\tWithUnmodifiedText(text).\n\t\tWithAutoRepeat(autoRepeat)\n\tif err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {\n\t\treturn fmt.Errorf(\"dispatching key event down: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (k *Keyboard) up(key string) error {\n\tkey = k.platformSpecificResolution(key)\n\n\tkeyInput := keyboardlayout.KeyInput(key)\n\tif _, ok := k.layout.ValidKeys[keyInput]; !ok {\n\t\treturn fmt.Errorf(\"'%s' is not a valid key for layout '%s'\", key, k.layoutName)\n\t}\n\n\tkeyDef := k.keyDefinitionFromKey(keyInput)\n\tk.modifiers &= ^k.modifierBitFromKeyName(keyDef.Key)\n\tk.pressedKeysMu.Lock()\n\tdelete(k.pressedKeys, keyDef.KeyCode)\n\tk.pressedKeysMu.Unlock()","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/keyboard.go#L115-L151","documentation":"Thrown by Keyboard.down() when the CDP Input.dispatchKeyEvent(KeyDown) action itself fails. Key validation already passed; the failure is at the protocol/transport layer: the session is closed, the target crashed, or the context was canceled (e.g. k6 timeout) while the event was being sent.","triggerScenarios":"down('Enter') after page.close() or context.close(); renderer crash mid-interaction; the k6 per-iteration/browser timeout canceling ctx between validation and dispatch; CDP connection dropped (browser process killed).","commonSituations":"Typing into a page whose navigation raced the keystroke; headless browser OOM-killed under load; test continuing after an earlier step already tore down the page.","solutions":["Guard with page.isClosed() before sending key events","Increase browser timeout options if the cause is a deadline (check the wrapped error for context.DeadlineExceeded)","Re-navigate or re-launch the page and retry the interaction from a known state","Investigate renderer crashes (check browser logs) rather than retrying blindly"],"exampleFix":"// before\nawait page.close();\nawait page.keyboard.down('Enter');\n\n// after\nif (!page.isClosed()) {\n  await page.keyboard.down('Enter');\n}","handlingStrategy":"try-catch","validationCode":"if (page.isClosed()) throw new Error('page closed before key down');\nif (!isValidKeyName(key)) throw new Error(`invalid key: ${key}`);","typeGuard":null,"tryCatchPattern":"try {\n  await page.keyboard.down(key);\n} catch (e) {\n  if (/dispatching key event down/.test(e.message)) {\n    if (/deadline|context/.test(e.message)) throw new Error('timeout during key down — raise timeout option');\n    throw e; // session/target gone — re-navigate before retrying\n  }\n  throw e;\n}","preventionTips":["Guard every key event with page.isClosed()","Size timeouts to cover the full interaction, not single events","Watch for renderer crashes in browser logs when events fail repeatedly"],"tags":["browser","keyboard","cdp","session-closed","transport"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}