Coder Social home page Coder Social logo

Comments (3)

OlliV avatar OlliV commented on April 27, 2024

That's sort of by design to avoid the overhead of the if as technically you just shouldn't write such code. Remember that the original intention was to receive and return tokens or instances.

from async-sema.

cardin avatar cardin commented on April 27, 2024

The example code was a bit extreme, but it's possible for this to occur accidentally.

If the developer was careless with either of the following, then there'll be more tokens than intended.

  • Pairing: Not one-to-one mapping of .acquire() to .release(), especially due to nested functions or error handling
  • Asynchronity: .acquire() occuring after .release() if async operations were not handled correctly
const sema = new Sema(1);
try {
   // code continues even if .acquire() is waiting or failed
   sema.acquire().then(x => accessDb(sema)).catch(e => {
      console.error(e);
      sema.release(); // unnecessary since finally{} will release it
   });
} finally {
   sema.release();
}
// At this point, there are 2 tokens.
accessDb(sema);

async function accessDb(sema) {
   sema.acquire().then(x => {
      // do something
      sema.release();
   });
}

I do get what you mean though. There's a lot of ways to use the semaphores incorrectly, we can't catch all of it.

But I felt that the semaphore count should at least be consistent, or give warnings if inconsistent. It is a sanity check, if you will. Otherwise it would pretty much go undetected.

from async-sema.

OlliV avatar OlliV commented on April 27, 2024

Yeah, that makes sense. I'm not against adding a check if it's seen as a useful addition.

from async-sema.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.