Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

fix(license): close() no longer throws after a license denial (JS, .NET)

CloakHQ25e05c67c5d4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 15386ef..75dacc3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ Changes are tagged: **[wrapper]** for Python/JS wrapper, **[binary]** for Chromi
- **[wrapper]** Launch with your own Playwright instance: `build_launch_options()` (JS `buildLaunchOptions()`, .NET `CloakLauncher.BuildLaunchOptionsAsync()`) returns the options `launch()` uses, so `pw.chromium.launch(**build_launch_options())` needs no copy of `launch()`. `humanize_browser()` / `humanize_browser_async()` apply the humanize layer to that browser. Python and .NET (JS had `buildLaunchOptions()` since 0.3.29) (#153, #255)
- **[wrapper]** Clear license errors for browsers you launch yourself: `guard_browser()` (JS `guardBrowser()`, .NET `CloakLauncher.GuardBrowser()`) turns a license denial after launch, like a full session limit, into `CloakBrowserLicenseError` instead of a bare "Target closed" error, and `license_error_from(e, opts)` (JS `licenseErrorFrom()`, .NET `CloakLauncher.LicenseErrorFrom()`) does the same for a denial during launch. Python, JavaScript, and .NET
- **[wrapper]** `license_denial(browser_or_context)` (JS `licenseDenial()`, .NET `LicenseDenial()` on the launch handles) reports whether the license refused a browser, without making a browser call. Works on everything the launch functions return, including Puppeteer browsers in JS. Python, JavaScript, and .NET
+- **[wrapper]** `close()` no longer throws after a license denial: a persistent context in JS, Puppeteer browsers, and .NET `LaunchContextAsync` handles raised `CloakBrowserLicenseError` from `close()`, and the JS ones stopped closing halfway. JavaScript and .NET
## [0.7.0] — 2026-11-01
diff --git a/dotnet/src/CloakBrowser/Handles.cs b/dotnet/src/CloakBrowser/Handles.cs
index 59fa992..8d8906a 100644
--- a/dotnet/src/CloakBrowser/Handles.cs
+++ b/dotnet/src/CloakBrowser/Handles.cs
@@ -270,6 +270,11 @@ internal class LicenseGuardProxy<T> : DispatchProxy, IGuardedProxy where T : cla
return value;
}
+ // Never guard teardown (mirrors the Python/JS skip of close): after a denial,
+ // close must still clean up, not throw the license error from a finally block.
+ if (targetMethod.Name is "CloseAsync" or "DisposeAsync")
+ return Forward(targetMethod, args);
+
var returnType = targetMethod.ReturnType;
if (returnType == typeof(Task))
diff --git a/dotnet/tests/CloakBrowser.Tests/LicenseTests.cs b/dotnet/tests/CloakBrowser.Tests/LicenseTests.cs
index dc99cc2..99f06d2 100644
--- a/dotnet/tests/CloakBrowser.Tests/LicenseTests.cs
+++ b/dotnet/tests/CloakBrowser.Tests/LicenseTests.cs
@@ -1331,6 +1331,20 @@ public class LicenseTests : IDisposable
Assert.NotNull(CloakLauncher.LicenseDenial(opts)); // cached after read
}
+ [Fact]
+ public async Task Guard_LeavesCloseAloneAfterDenial()
+ {
+ // Close in a finally block must clean up, not throw the license error.
+ var f = Path.Combine(_tmp, "d.json");
+ File.WriteAllText(f, "76");
+ var (raw, rec) = CloakBrowser.Tests.Wrappers.Fake.Of<Microsoft.Playwright.IBrowser>();
+ var guarded = (Microsoft.Playwright.IBrowser)LicenseGuard.Wrap(raw, f);
+
+ await guarded.CloseAsync();
+ Assert.True(rec.WasCalled("CloseAsync"));
+ await Assert.ThrowsAsync<CloakBrowserLicenseError>(() => guarded.NewPageAsync());
+ }
+
[Fact]
public void GuardBrowser_NoLicenseReturnsSameBrowser()
{
diff --git a/js/src/license.ts b/js/src/license.ts
index 98ab65a..11c33e5 100644
--- a/js/src/license.ts
+++ b/js/src/license.ts
@@ -233,8 +233,11 @@ const GUARD_FACTORY_METHODS = [
// surface is called INTERNALLY by playwright/puppeteer (e.g. emit('close')
// during teardown), so throwing a license error from inside their own event
// dispatch would crash the driver rather than surface cleanly to the user.
+// Teardown and state queries are skipped too: Playwright's BrowserContext.close()
+// calls page.isClosed() and Puppeteer's Browser.close() ends with disconnect();
+// guarding them made close() throw after a denial. process() is a state query.
const GUARD_SKIP_METHODS = new Set([
- "close",
+ "close", "isClosed", "disconnect", "process",
"on", "off", "once", "emit", "addListener", "removeListener",
"removeAllListeners", "listeners", "rawListeners", "listenerCount",
"eventNames", "prependListener", "prependOnceListener",
diff --git a/js/tests/launch.test.ts b/js/tests/launch.test.ts
index 3026728..a6d77ae 100644
--- a/js/tests/launch.test.ts
+++ b/js/tests/launch.test.ts
@@ -244,6 +244,31 @@ describe("composable Playwright launch helpers", () => {
expect(licenseDenial(ctx)).toBeInstanceOf(CloakBrowserLicenseError); // cached after read
});
+ it("guard leaves close() and isClosed() alone after a denial", async () => {
+ // Playwright's BrowserContext.close() calls page.isClosed() internally; guarding
+ // it made close() throw the license error and abort teardown.
+ const { installLicenseGuard } = await import("../src/license.js");
+ const fs = await import("node:fs");
+ const os = await import("node:os");
+ const path = await import("node:path");
+
+ const denial = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "cb-")), "d.json");
+ fs.writeFileSync(denial, "76");
+ const page: any = { isClosed: () => false, close: async () => "closed", goto: async () => "ok" };
+ installLicenseGuard(page, denial);
+
+ expect(page.isClosed()).toBe(false);
+ await expect(page.close()).resolves.toBe("closed");
+ await expect(page.goto()).rejects.toThrow(/session limit/);
+
+ // Puppeteer: Browser.close() ends with disconnect().
+ const browser: any = { disconnect: async () => "gone", process: () => null, newPage: async () => ({}) };
+ installLicenseGuard(browser, denial);
+ await expect(browser.disconnect()).resolves.toBe("gone");
+ expect(browser.process()).toBeNull();
+ await expect(browser.newPage()).rejects.toThrow(/session limit/);
+ });
+
it("guardBrowser is a no-op without a license", async () => {
const { guardBrowser } = await import("../src/index.js");
const newPage = async () => "page";