Thread-Unsafe Singleton Pattern

Detects singleton implementations that are not thread-safe and can result in multiple instances being created under concurrent access.

zakirkun 5c07495 2 files · 1.6 KB Updated

File contents

Thread-Unsafe Singleton

Overview

Lazy-initialized singletons without thread synchronization can result in multiple instances being created when the class is initialized concurrently by multiple threads. This causes inconsistent state, duplicate resource consumption, and potential security issues if security-related objects (loggers, auth managers) are duplicated.

Remediation

  • Java: Use enum, static initialization block, or synchronized + volatile
  • Go: Use sync.Once
  • Python: Use module-level variable (thread-safe by GIL) or threading.Lock

zakirkun/ice-tea/tree/main/skills/race-condition/unsafe-singleton commit 5c0749513d

Frequently asked questions

npx skillmds@latest add zakirkun/thread-unsafe-singleton-pattern