@imqueue/rpc 3.4.0 → 3.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -8,6 +8,28 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ### Fixed
12
+
13
+ - **A client created after another one was destroyed lost its first reply.**
14
+ `pid()` allocates the lowest free identifier, and that identifier is the only
15
+ part of a client's queue name that separates two clients of the same service
16
+ on one host — so an identifier released by `forgetPid()` handed the whole
17
+ queue name to the next client. The reader of the destroyed client stays
18
+ blocked on that queue past `destroy()`, consumes the first message addressed
19
+ to its successor and discards it, leaving the caller waiting forever with no
20
+ error. Identifiers this process has given back are no longer re-used.
21
+
22
+ This is what made `IMQClient.create()` look broken: generating a client
23
+ starts a temporary client to read the service description and destroys it, so
24
+ the first call through *any* client built afterwards in that process — a
25
+ runtime-generated one or a statically generated one — never returned. The
26
+ second call onwards worked, because only one message is swallowed.
27
+
28
+ Identifiers freed by another process, or left behind by one that died, are
29
+ still re-used as before.
30
+
31
+ ## [3.4.0] - 2026-07-26
32
+
11
33
  ### Changed
12
34
 
13
35
  - **On a delayed call, a trailing `undefined` is never delivered.** Every
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/rpc",
3
- "version": "3.4.0",
3
+ "version": "3.4.2",
4
4
  "description": "Type-safe RPC framework for Node.js & TypeScript microservices over a Redis message queue — self-describing services generate their own strongly-typed clients (@imqueue framework)",
5
5
  "keywords": [
6
6
  "redis",
@@ -51,7 +51,7 @@
51
51
  "author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
52
52
  "license": "GPL-3.0-only",
53
53
  "dependencies": {
54
- "@imqueue/core": "^3.2.1",
54
+ "@imqueue/core": "^3.2.2",
55
55
  "acorn": "^8.17.0",
56
56
  "typescript": "^7.0.2"
57
57
  },
@@ -15,13 +15,18 @@ export declare const IMQ_PID_DIR: string;
15
15
  * Returns an increment-based process identifier for the given service name,
16
16
  * creating the corresponding pid file under the given directory.
17
17
  *
18
+ * Identifiers this process has released through `forgetPid()` are skipped, see
19
+ * `retiredIds`.
20
+ *
18
21
  * @param {string} name - name of the service to create the pid file for
19
22
  * @param {string} [path] - directory to store the pid file in
20
23
  * @returns {number} - the allocated increment-based identifier
21
24
  */
22
25
  export declare function pid(name: string, path?: string): number;
23
26
  /**
24
- * Removes the pid file for the given service name and identifier.
27
+ * Removes the pid file for the given service name and identifier, and records
28
+ * the identifier as retired so that `pid()` does not hand it out again for the
29
+ * lifetime of this process, see `retiredIds`.
25
30
  *
26
31
  * @param {string} name - name of the service whose pid file to remove
27
32
  * @param {number} id - increment-based identifier of the pid file
@@ -36,10 +36,40 @@ export const IMQ_TMP_DIR = process.env.TMPDIR || '/tmp';
36
36
  * Directory where imq-rpc stores its pid files.
37
37
  */
38
38
  export const IMQ_PID_DIR = resolve(IMQ_TMP_DIR, '.imq-rpc');
39
+ /**
40
+ * Identifiers this process has already allocated and given back, per pid file
41
+ * location. They are never handed out again while the process lives.
42
+ *
43
+ * The identifier is the only part of a client's queue name that distinguishes
44
+ * two clients of the same service on the same host, so re-using one re-uses the
45
+ * queue name. Handing a name back within the same process is not safe: the
46
+ * queue of the client that just released it still has a reader blocked on it,
47
+ * which outlives `destroy()` and consumes the first message addressed to the
48
+ * new owner — that reply is then lost, and its caller waits forever.
49
+ *
50
+ * Identifiers freed by *another* process (or left behind by one that died) are
51
+ * still re-used, which is what keeps names dense across restarts.
52
+ */
53
+ const retiredIds = new Map();
54
+ /**
55
+ * Returns the key under which retired identifiers are tracked. Includes the
56
+ * path so that pid files kept in different directories cannot influence each
57
+ * other.
58
+ *
59
+ * @param {string} name - name of the service
60
+ * @param {string} path - directory the pid files are stored in
61
+ * @returns {string}
62
+ */
63
+ function retiredKey(name, path) {
64
+ return `${path}/${name}`;
65
+ }
39
66
  /**
40
67
  * Returns an increment-based process identifier for the given service name,
41
68
  * creating the corresponding pid file under the given directory.
42
69
  *
70
+ * Identifiers this process has released through `forgetPid()` are skipped, see
71
+ * `retiredIds`.
72
+ *
43
73
  * @param {string} name - name of the service to create the pid file for
44
74
  * @param {string} [path] - directory to store the pid file in
45
75
  * @returns {number} - the allocated increment-based identifier
@@ -50,9 +80,14 @@ export function pid(name, path = IMQ_PID_DIR) {
50
80
  if (!existsSync(path)) {
51
81
  mkdirSync(path);
52
82
  }
83
+ const retired = retiredIds.get(retiredKey(name, path));
53
84
  let id = 0;
54
85
  let done = false;
55
86
  while (!done) {
87
+ if (retired?.has(id)) {
88
+ id++;
89
+ continue;
90
+ }
56
91
  try {
57
92
  writeFileSync(`${pidFile}-${id}.pid`, process.pid + '', pidOpts);
58
93
  done = true;
@@ -69,7 +104,9 @@ export function pid(name, path = IMQ_PID_DIR) {
69
104
  return id;
70
105
  }
71
106
  /**
72
- * Removes the pid file for the given service name and identifier.
107
+ * Removes the pid file for the given service name and identifier, and records
108
+ * the identifier as retired so that `pid()` does not hand it out again for the
109
+ * lifetime of this process, see `retiredIds`.
73
110
  *
74
111
  * @param {string} name - name of the service whose pid file to remove
75
112
  * @param {number} id - increment-based identifier of the pid file
@@ -83,5 +120,13 @@ export function forgetPid(name, id, logger, path = IMQ_PID_DIR) {
83
120
  catch {
84
121
  /* ignore */
85
122
  }
123
+ const key = retiredKey(name, path);
124
+ const retired = retiredIds.get(key);
125
+ if (retired) {
126
+ retired.add(id);
127
+ }
128
+ else {
129
+ retiredIds.set(key, new Set([id]));
130
+ }
86
131
  }
87
132
  //# sourceMappingURL=pid.js.map