@std/async/unstable-semaphore should be bind methods in the constructor
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
I'm an idiot and passed .release directly into a promise chains .finally()
```typescript
sema.acquire().then(() => somethingAsync()).finally(sema.release)
```
this means i get this error
```
error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '#head')
if (this.#head) {
^
at release (https://jsr.io/@std/async/1.4.0/unstable_semaphore.ts:139:14)
at
```
however this somehow worked(?) but inconsistently. so it should be changed, by binding in the constructor `this.release = this.release.bind(this)`, or by fixing whatever makes it inconsistently work. I have no idea why it sometimes works. Seems to have a higher chance of working when the semaphore max is higher, but it works even when the total number of acquire/release calls is more than max so idk.
Contributor guide
Research direction
Start with @std/async/1.4.0/unstable_semaphore.ts, especially the Semaphore constructor and release method around the reported line. Reproduce the promise-chain example using sema.release as finally’s callback and compare it with direct invocation; done means the callback works reliably without an undefined this context.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100