grafana/k6 · error

clearing permissions

Error message

clearing permissions: %w

What it means

ClearPermissions runs the CDP command Browser.resetPermissions scoped to this context's ID (cdpbrowser.ResetPermissions().WithBrowserContextID(b.id)); this error means the CDP call itself failed. Typical wrapped causes: the browser context ID is unknown to the browser (already disposed) or the command channel to Chromium is gone.

Solutions

  1. Call clearPermissions() during the test, before closing the context, not in teardown after close
  2. Wrap cleanup-path calls in try/catch and treat failures on already-closed contexts as non-fatal
  3. Confirm the browser and context are still open before the call
  4. If the wrapped error indicates a dead connection, restart the browser instead of retrying

Example fix

// before
// teardown: context already closed by k6
ctx.clearPermissions(); // throws 'clearing permissions'

// after
// grant and clear within the test body, before close
ctx.grantPermissions(['geolocation']);
// ... assertions ...
ctx.clearPermissions();
ctx.close();
Defensive patterns

Strategy: try-catch

Try / catch

// cleanup-path call: tolerate failure on already-closed contexts
try {
  ctx.clearPermissions();
} catch (e) {
  console.warn(`clearPermissions ignored during cleanup: ${e.message}`);
}

Prevention

When it happens

Trigger: Calling browserContext.clearPermissions() after the context was closed/disposed, after browser.close() or a browser crash, or while the CDP WebSocket connection is down.

Common situations: Permission cleanup code in a finally block or teardown() that runs after k6 already closed the context; browser process dying mid-test; double cleanup (user code plus framework teardown).

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/dd82c57938a81323. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:199

			return fmt.Errorf("adding init script to browser context: %w", err)
		}
	}

	return nil
}

// Browser returns the browser instance that this browser context belongs to.
func (b *BrowserContext) Browser() *Browser {
	return b.browser
}

// ClearPermissions clears any permission overrides.
func (b *BrowserContext) ClearPermissions() error {
	b.logger.Debugf("BrowserContext:ClearPermissions", "bctxid:%v", b.id)

	action := cdpbrowser.ResetPermissions().WithBrowserContextID(b.id)
	if err := action.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {
		return fmt.Errorf("clearing permissions: %w", err)
	}

	return nil
}

// Close shuts down the browser context.
func (b *BrowserContext) Close() error {
	b.logger.Debugf("BrowserContext:Close", "bctxid:%v", b.id)

	if b.id == "" {
		return fmt.Errorf("default browser context can't be closed")
	}
	if err := b.browser.disposeContext(b.id); err != nil {
		return fmt.Errorf("disposing browser context: %w", err)
	}
	return nil
}

View on GitHub (pinned to 01ffac6f24)