Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Reset single-flight/processing flags in a finally block
Clear locks and in-progress flags in finally so a throwing catch handler cannot leave them stuck set.
Bad example
| 1 | this.isProcessing = true; |
| 2 | try { |
| 3 | await this.process(batch); |
| 4 | } catch (error) { |
| 5 | // If this throw escapes, isProcessing is never reset |
| 6 | this.logger.error('failed', error); |
| 7 | } |
| 8 | this.isProcessing = false; // skipped on a throwing catch |
Explanation (EN)
If the catch body throws, the inline reset after the try/catch is skipped and the flag stays true, so the single-flight loop never resumes and processing silently stops.
Objašnjenje (HR)
Ako tijelo catch-a baci grešku, reset nakon try/catch se preskoči i zastavica ostaje true, pa se single-flight petlja nikad ne nastavi i obrada se tiho zaustavi.
Good example
| 1 | this.isProcessing = true; |
| 2 | try { |
| 3 | await this.process(batch); |
| 4 | } catch (error) { |
| 5 | this.logger.error('failed', error); |
| 6 | } finally { |
| 7 | this.isProcessing = false; // always runs |
| 8 | } |
Explanation (EN)
Putting the reset in finally guarantees the flag is cleared even if the catch handler throws, keeping the loop alive.
Objašnjenje (HR)
Stavljanje reseta u finally jamči da se zastavica očisti čak i ako catch handler baci grešku, čime petlja ostaje živa.