Skip to content

Commit fd28172

Browse files
fix(async/unstable): validate Semaphore max is a positive integer (#6934)
1 parent b256b4c commit fd28172

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

async/unstable_semaphore.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ export class Semaphore {
3636
* Creates a new semaphore with the specified number of permits.
3737
*
3838
* @param max Maximum concurrent permits. Defaults to 1 (mutex).
39+
* @throws {TypeError} If `max` is not a positive integer.
3940
*/
4041
constructor(max: number = 1) {
41-
if (max < 1) {
42+
if (!Number.isInteger(max) || max < 1) {
4243
throw new TypeError(
43-
`Cannot create semaphore as 'max' must be at least 1: current value is ${max}`,
44+
`Cannot create semaphore as 'max' must be a positive integer: received ${max}`,
4445
);
4546
}
4647
this.#count = this.#max = max;

async/unstable_semaphore_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Deno.test("Semaphore constructor throws for non-positive max", () => {
2828
assertThrows(() => new Semaphore(-1), TypeError);
2929
});
3030

31+
Deno.test("Semaphore constructor throws for non-integer max", () => {
32+
assertThrows(() => new Semaphore(NaN), TypeError);
33+
assertThrows(() => new Semaphore(Infinity), TypeError);
34+
assertThrows(() => new Semaphore(1.5), TypeError);
35+
});
36+
3137
Deno.test("Semaphore constructor defaults to 1", async () => {
3238
const sem = new Semaphore();
3339
await sem.acquire();

0 commit comments

Comments
 (0)