directus / directus/directus

Hooks may be registered multiple times when enabling/disabling extensions

Open
#25,690 1 comment 0 reactions 0 assignees View on GitHub
Bug Engine Extensions High Reach Low Impact
Dominant language
TypeScript
Stars
37.9k
Forks
4.9k
Avg merge
3d 21h
Merged PRs (30d)
36

Description

### Describe the Bug

**Description:**
When developing a Directus extension with a hook that registers an action (e.g., items.create), the hook may be executed multiple times if the extension is disabled and then re-enabled in the admin settings. This causes the same action handler to be registered multiple times, leading to duplicate executions of the handler for a single event.

Currently, the register.action() API does not provide a way to check if an action is already registered or to remove/unregister a previously registered handler.

**Expected Behavior:**

The action should only be registered once per server instance, even after toggling the extension.
Ideally, the register.action() API would provide a mechanism to check for existing registrations or to remove/unregister handlers.

**Impact:**
- Duplicate execution of hooks
- Potential duplicate writes to the database
- Confusing behavior when enabling/disabling extensions

### To Reproduce

1.Create a hook that registers an action using register.action('items.create', handler).
```js
// Example code
import { defineHook } from '@directus/extensions-sdk'

export default defineHook(async (register, context) => {
context.logger.info('[example-hook] Registering example hook...');

const handler = async (meta, eventContext) => {
const itemId = meta.key as string;
context.logger.info(`[example-hook] Handler triggered ${itemId}`);
};

register.action('items.create', handler);
})
```
2. Enable the extension in Directus.
3. Disable and then re-enable the extension 2 times.
4. Create an item in Directus.
5. Observe that the action handler executes multiple times for the same item creation.
```
directus-1 | [13:30:57.609] INFO: [example-hook] Handler triggered 3
directus-1 | [13:30:57.609] INFO: [example-hook] Handler triggered 3
directus-1 | [13:30:57.609] INFO: [example-hook] Handler triggered 3
```

### Directus Version

v11.10.2

### Hosting Strategy

Self-Hosted (Docker Image)

### Database

mysql:8

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.