Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
    • Event Scheduling Puzzles
    • Race Condition Puzzles
    • Clocking Block Puzzles
    • Semaphore & Mailbox Puzzles
      • Lock Held Across Blocking Response Wait
      • Bounded Mailbox put Deadlock
      • try_get Busy Spin at Time 0
      • Leaked Semaphore Token on Error Path
      • Two Consumers Waiting on One Mailbox
      • Deadlock-Free Request Loop with try_put/try_get
    • Concurrency & Timing Puzzles: Tricky Q&A
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←Semaphore & Mailbox Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. Concurrency & Timing Puzzles
  4. Semaphore & Mailbox Puzzles
  5. Leaked Semaphore Token on Error Path
On this page

    VLSI DV Interview PuzzlesAll levels

    Leaked Semaphore Token on Error Path

    Predict the output. Does worker B wait or time out when it tries for the token, and what does the missing put cost?

    Puzzle

    Difficulty: Easy · Puzzle 4 of 6 · Topic: Semaphore & Mailbox Puzzles

    Predict the output. Does worker B wait or time out when it tries for the token, and what does the missing put cost?

    Code

    tb.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    module tb;
      semaphore sem = new(1);
    
      task worker_a();
        sem.get(1);
        $display("[%0t] A got token", $time);
        if (1) return; // missing sem.put(1)
      endtask
    
      task worker_b();
        int ok;
        #10;
        ok = sem.try_get(1);
        $display("[%0t] B try_get=%0d", $time, ok);
      endtask
    
      initial fork
        worker_a();
        worker_b();
      join
    endmodule

    Hint

    try_get never blocks and never times out: it returns 0 at once if no token is available.

    Step-by-step solution

    Diagram
    1) A gets the only token at t=0 and returns without put.
    2) The token count stays 0.
    3) At t=10 B calls try_get(1). It returns 0 immediately; nothing 'times out'.
    4) A blocking sem.get(1) in B would hang forever here.

    Answer

    Answer: Prints [0] A got token and [10] B try_get=0. The leaked token means every later get blocks forever.

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. try_get returns 0 at once; the leaked token is never returned. A blocking get in B would wait forever on the leaked token.

    Why candidates get it wrong

    B does not time out; try_get is non-blocking and simply returns 0. A blocking get would be the silent hang, which is why leaks are deadlocks in slow motion.

    Interviewer follow-up

    How would you enforce token release on every exit path? Model answer: structure the task so there is one exit: acquire, do the work in a block without early return, then put (or wrap the body in a task that returns a status and always call put after it).

    Say it out loud

    A gets the only token at zero and returns without putting it back, so the count stays at zero. At ten, B's try_get returns 0 immediately, because it is non-blocking and does not time out. A blocking get would hang forever, which is why leaks are deadlocks in slow motion. The trap is expecting B to wait or time out. I would put the token back on every exit path, and check the count at the end of the test.

    Related topics

    • Semaphores: One Key and Many

    • Synchronization Patterns: Handshakes, Phasing and Objections

    • Back to Semaphore & Mailbox Puzzles

    • Concurrency & Timing Puzzles section

    Previous

    82 / 293

    Next: Two Consumers Waiting on One Mailbox

    Up next

    Up nextTwo Consumers Waiting on One Mailbox

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Lock Held Across Blocking Response Wait
    • Bounded Mailbox put Deadlock
    • Deadlock-Free Request Loop with try_put/try_get