arch(modules): installable modules must be stateless -- expose M.new() for stateful operations #124

Open
opened 2026-06-04 09:20:30 +02:00 by michael · 1 comment
Owner

Principle

Installable DAW modules MUST be stateless.

Modules are loaded once via require() and cached by Lua. If a module
holds state in module-level variables, all callers share that state.
This makes modules non-instantiable and causes subtle bugs when the
same module is used for multiple purposes simultaneously.

Rule

Installable modules (managed by the module manager, living in
var/modules/) MUST NOT hold state in module-level variables.

If a module needs state (e.g. an open file handle), it MUST expose
a constructor:

function M.new(config)
  local instance = {}
  local _state = ...

  function instance.operation() ... end

  return instance
end

The caller (adapter) is responsible for managing instances.

Known violation

daw.log.file (DAW/daw-modul-log-file) holds a file handle in a
module-level variable:

local _handle = nil

Fixed in DAW/daw-modul-log-file (separate issue).

Scope

  • All modules in var/modules/ (installed via module manager)
  • Does NOT apply to daw.common.* (library modules, stateless by design)
  • Does NOT apply to fragjan core modules (program-internal)

Acceptance criteria

  • fragjan rule added: installed modules must not use module-level
    mutable state (no module-level variables that change after load)
  • daw.log.file fixed (DAW/daw-modul-log-file)
  • all existing modules reviewed for state violations

References

  • DAW/fragjan#123 -- no hardcoded module references
  • DAW/daw-modul-log-file (fix issue)
## Principle Installable DAW modules MUST be stateless. Modules are loaded once via require() and cached by Lua. If a module holds state in module-level variables, all callers share that state. This makes modules non-instantiable and causes subtle bugs when the same module is used for multiple purposes simultaneously. ## Rule Installable modules (managed by the module manager, living in var/modules/) MUST NOT hold state in module-level variables. If a module needs state (e.g. an open file handle), it MUST expose a constructor: ```lua function M.new(config) local instance = {} local _state = ... function instance.operation() ... end return instance end ``` The caller (adapter) is responsible for managing instances. ## Known violation daw.log.file (DAW/daw-modul-log-file) holds a file handle in a module-level variable: ```lua local _handle = nil ``` Fixed in DAW/daw-modul-log-file (separate issue). ## Scope - All modules in var/modules/ (installed via module manager) - Does NOT apply to daw.common.* (library modules, stateless by design) - Does NOT apply to fragjan core modules (program-internal) ## Acceptance criteria - [ ] fragjan rule added: installed modules must not use module-level mutable state (no module-level variables that change after load) - [ ] daw.log.file fixed (DAW/daw-modul-log-file) - [ ] all existing modules reviewed for state violations ## References - DAW/fragjan#123 -- no hardcoded module references - DAW/daw-modul-log-file (fix issue)
michael added this to the Basic Version milestone 2026-06-04 11:29:28 +02:00
Author
Owner

Semantic static analysis required for this rule -- blocked on DAW/fragjan#107 (scanner primitive). Moving to later milestone.

Semantic static analysis required for this rule -- blocked on DAW/fragjan#107 (scanner primitive). Moving to later milestone.
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
DAW/fragjan#124
No description provided.