grafana/k6 · error

dispatching key event down: %w

Error message

dispatching key event down: %w

What it means

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.

Source

Thrown at internal/js/modules/k6/browser/common/keyboard.go:133

	k.pressedKeysMu.Unlock()

	keyType := input.KeyDown
	if text == "" {
		keyType = input.KeyRawDown
	}

	action := input.DispatchKeyEvent(keyType).
		WithModifiers(input.Modifier(k.modifiers)).
		WithKey(keyDef.Key).
		WithWindowsVirtualKeyCode(keyDef.KeyCode).
		WithCode(keyDef.Code).
		WithLocation(keyDef.Location).
		WithIsKeypad(keyDef.Location == 3).
		WithText(text).
		WithUnmodifiedText(text).
		WithAutoRepeat(autoRepeat)
	if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
		return fmt.Errorf("dispatching key event down: %w", err)
	}

	return nil
}

func (k *Keyboard) up(key string) error {
	key = k.platformSpecificResolution(key)

	keyInput := keyboardlayout.KeyInput(key)
	if _, ok := k.layout.ValidKeys[keyInput]; !ok {
		return fmt.Errorf("'%s' is not a valid key for layout '%s'", key, k.layoutName)
	}

	keyDef := k.keyDefinitionFromKey(keyInput)
	k.modifiers &= ^k.modifierBitFromKeyName(keyDef.Key)
	k.pressedKeysMu.Lock()
	delete(k.pressedKeys, keyDef.KeyCode)
	k.pressedKeysMu.Unlock()

View on GitHub (pinned to 93accf6570)

Solutions

  1. Guard with page.isClosed() before sending key events
  2. Increase browser timeout options if the cause is a deadline (check the wrapped error for context.DeadlineExceeded)
  3. Re-navigate or re-launch the page and retry the interaction from a known state
  4. Investigate renderer crashes (check browser logs) rather than retrying blindly

Example fix

// before
await page.close();
await page.keyboard.down('Enter');

// after
if (!page.isClosed()) {
  await page.keyboard.down('Enter');
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (page.isClosed()) throw new Error('page closed before key down');
if (!isValidKeyName(key)) throw new Error(`invalid key: ${key}`);

Try / catch

try {
  await page.keyboard.down(key);
} catch (e) {
  if (/dispatching key event down/.test(e.message)) {
    if (/deadline|context/.test(e.message)) throw new Error('timeout during key down — raise timeout option');
    throw e; // session/target gone — re-navigate before retrying
  }
  throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/e5d15e22f7574998. Report an issue: GitHub.