@hitrading/ctp-node 4.1.1 → 4.1.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 +14 -0
- package/docs/native-hooks.md +12 -4
- package/docs/native-hooks.zh-CN.md +9 -4
- package/package.json +1 -1
- package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
- package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
- package/src/native/mdapi.cc +24 -12
- package/src/native/traderapi.cc +33 -16
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [4.1.2](https://github.com/hitrading/ctp-node/compare/v4.1.1...v4.1.2) (2026-11-13)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **native:** CTP API 推迟到 start() 创建,未启动的实例关闭时不再调用 Release() ([266a1a8](https://github.com/hitrading/ctp-node/commit/266a1a84842d489ba2b06712a11d134beb990a6a))
|
|
7
|
+
|
|
8
|
+
## [4.1.2-rc.1](https://github.com/hitrading/ctp-node/compare/v4.1.1...v4.1.2-rc.1) (2026-11-13)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **native:** CTP API 推迟到 start() 创建,未启动的实例关闭时不再调用 Release() ([266a1a8](https://github.com/hitrading/ctp-node/commit/266a1a84842d489ba2b06712a11d134beb990a6a))
|
|
14
|
+
|
|
1
15
|
## [4.1.1](https://github.com/hitrading/ctp-node/compare/v4.1.0...v4.1.1) (2026-11-13)
|
|
2
16
|
|
|
3
17
|
|
package/docs/native-hooks.md
CHANGED
|
@@ -209,19 +209,27 @@ missing metadata fails closed. Live soak remains a separate release gate.
|
|
|
209
209
|
## Process lifecycle: create once, reuse
|
|
210
210
|
|
|
211
211
|
`MarketData` and `Trader` are **create-once, reuse-for-the-life-of-the-process**
|
|
212
|
-
objects. The wrapper constructor calls the native `_start()`, which
|
|
213
|
-
vendor `
|
|
212
|
+
objects. The wrapper constructor calls the native `_start()`, which creates the
|
|
213
|
+
vendor API (`CreateFtdcMdApi` / `CreateFtdcTraderApi`) and immediately calls its
|
|
214
|
+
`Init()`; `close()` calls the vendor `Release()`
|
|
214
215
|
(`CThostFtdcMdApi::Release()` / `CThostFtdcTraderApi::Release()` — see
|
|
215
216
|
`doClose()` in `src/native/mdapi.cc` and `src/native/traderapi.cc`).
|
|
216
217
|
|
|
218
|
+
**Never `Release()` an API that was not `Init()`ed.** The vendor `Release()`
|
|
219
|
+
joins an internal reactor thread that only `Init()` starts; on a never-initialised
|
|
220
|
+
instance it takes an undefined teardown path that segfaults on Ubuntu 26.04
|
|
221
|
+
(glibc 2.43). The native objects therefore create the vendor API only inside
|
|
222
|
+
`_start()`, right before `Init()`: a raw object that is closed or garbage-collected
|
|
223
|
+
without `_start()` owns no vendor object, and its `close()` releases nothing.
|
|
224
|
+
|
|
217
225
|
**The observed hazard.** On the bundled Windows vendor DLLs, repeated
|
|
218
226
|
`Init()`→`Release()` cycles in one process have reproducibly hung inside
|
|
219
227
|
`Release()` — empirically after 4–11 cycles, varying with timing and CTP's
|
|
220
228
|
internal reconnect-thread state. The binding cannot recover a process once a
|
|
221
229
|
closed-source vendor call is stuck, so this candidate deliberately adopts a
|
|
222
230
|
create-once lifecycle. (For comparison: the raw native object *without*
|
|
223
|
-
`_start()` survives
|
|
224
|
-
`Init()`+`Release()` pairing. A front like `tcp://127.0.0.1:1` that never
|
|
231
|
+
`_start()` survives any number of create/close cycles, since it never creates a
|
|
232
|
+
vendor API; the trigger is specifically the `Init()`+`Release()` pairing. A front like `tcp://127.0.0.1:1` that never
|
|
225
233
|
connects makes it worse, because CTP's reconnect machinery is what wedges.)
|
|
226
234
|
|
|
227
235
|
**The rule: never reconnect by destroying and recreating the client.** CTP
|
|
@@ -153,14 +153,19 @@ armed.disarm(); // 移除触发器
|
|
|
153
153
|
## 进程生命周期 一次创建并复用
|
|
154
154
|
|
|
155
155
|
`MarketData` 和 `Trader` 是**一次创建、进程内一直复用**的对象。包装构造函数调用原生 `_start()`,
|
|
156
|
-
|
|
157
|
-
`
|
|
158
|
-
`doClose()`)。
|
|
156
|
+
后者创建厂商 API(`CreateFtdcMdApi` / `CreateFtdcTraderApi`)并立即调用其 `Init()`;`close()` 调用
|
|
157
|
+
厂商 `Release()`(`CThostFtdcMdApi::Release()` / `CThostFtdcTraderApi::Release()`——见
|
|
158
|
+
`src/native/mdapi.cc` 和 `src/native/traderapi.cc` 里的 `doClose()`)。
|
|
159
|
+
|
|
160
|
+
**绝不对未 `Init()` 的 API 调用 `Release()`。** 厂商 `Release()` 会 join 一个只有 `Init()` 才启动的
|
|
161
|
+
内部反应器线程;对从未初始化的实例,它会走未定义的销毁路径,在 Ubuntu 26.04(glibc 2.43)上段错误。
|
|
162
|
+
因此原生对象只在 `_start()` 里、紧挨 `Init()` 之前创建厂商 API:未经 `_start()` 就被关闭或回收的
|
|
163
|
+
裸原生对象不持有任何厂商对象,其 `close()` 不释放任何东西。
|
|
159
164
|
|
|
160
165
|
**已观测风险。** 在当前随包的 Windows 厂商 DLL 上,同一进程反复
|
|
161
166
|
`Init()`→`Release()` 会可重现地卡在 `Release()` 内部——经验上 4–11 次,随时机和 CTP 内部
|
|
162
167
|
重连线程状态而变。闭源厂商调用一旦卡住,本绑定无法让该进程恢复,因此当前候选版明确采用
|
|
163
|
-
“一次创建”生命周期。(对照:*不*调用 `_start()`
|
|
168
|
+
“一次创建”生命周期。(对照:*不*调用 `_start()` 的裸原生对象能扛过任意次 create/close 循环,因为它根本不创建厂商 API;触发点
|
|
164
169
|
正是 `Init()`+`Release()` 这一对。一个永不连上的前置(如 `tcp://127.0.0.1:1`)会让它更糟,因为
|
|
165
170
|
卡住的正是 CTP 的重连机制。)
|
|
166
171
|
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
package/src/native/mdapi.cc
CHANGED
|
@@ -163,7 +163,12 @@ private:
|
|
|
163
163
|
Napi::Value BurstTicks(const Napi::CallbackInfo &info);
|
|
164
164
|
Napi::Value ProduceTestTicks(const Napi::CallbackInfo &info);
|
|
165
165
|
|
|
166
|
+
// Non-null only between _start() (CreateFtdcMdApi + Init) and close(): CTP's
|
|
167
|
+
// Release() on an instance that never ran Init() joins a reactor thread that
|
|
168
|
+
// was never started, which crashes on newer glibc (Ubuntu 26.04 / 2.43).
|
|
166
169
|
CThostFtdcMdApi *api_ = nullptr;
|
|
170
|
+
std::string flowPath_;
|
|
171
|
+
std::vector<std::string> fronts_;
|
|
167
172
|
MdSpi *spi_ = nullptr;
|
|
168
173
|
EventChannel *ch_ = nullptr;
|
|
169
174
|
std::shared_ptr<ArmRegistry> armReg_;
|
|
@@ -248,21 +253,17 @@ MarketData::MarketData(const Napi::CallbackInfo &info)
|
|
|
248
253
|
return;
|
|
249
254
|
}
|
|
250
255
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
+
// The CTP API itself is created in _start(), immediately before Init() (see
|
|
257
|
+
// api_): an instance that is closed or finalized without ever starting then
|
|
258
|
+
// owns no CTP object, so close() never calls Release() on an un-Init()ed API.
|
|
259
|
+
flowPath_ = std::move(flowPath);
|
|
260
|
+
fronts_ = std::move(fronts);
|
|
256
261
|
// Market data drops the OLDEST record under backpressure: a full-market feed
|
|
257
262
|
// wants the freshest quote, not a stale one, when the JS loop falls behind.
|
|
258
263
|
ch_ = new EventChannel(8192, static_cast<size_t>(ctpMaxStructSize()),
|
|
259
264
|
DropPolicy::Oldest);
|
|
260
265
|
spi_ = new MdSpi(ch_);
|
|
261
266
|
spi_->setSnapshotCache(snap_.get());
|
|
262
|
-
api_->RegisterSpi(spi_);
|
|
263
|
-
|
|
264
|
-
for (auto &addr : fronts)
|
|
265
|
-
api_->RegisterFront(const_cast<char *>(addr.c_str()));
|
|
266
267
|
}
|
|
267
268
|
|
|
268
269
|
MarketData::~MarketData() { doClose(); }
|
|
@@ -519,7 +520,7 @@ Napi::Value MarketData::ReqQryMulticastInstrument(
|
|
|
519
520
|
|
|
520
521
|
Napi::Value MarketData::Start(const Napi::CallbackInfo &info) {
|
|
521
522
|
Napi::Env env = info.Env();
|
|
522
|
-
if (started_ || !
|
|
523
|
+
if (started_ || closed_ || !ch_)
|
|
523
524
|
return env.Undefined();
|
|
524
525
|
napi_status st = ch_->start(env, info[0]);
|
|
525
526
|
if (st != napi_ok) {
|
|
@@ -527,8 +528,19 @@ Napi::Value MarketData::Start(const Napi::CallbackInfo &info) {
|
|
|
527
528
|
.ThrowAsJavaScriptException();
|
|
528
529
|
return env.Undefined();
|
|
529
530
|
}
|
|
530
|
-
|
|
531
|
-
|
|
531
|
+
started_ = true; // the channel is live; never start it twice, even on failure below
|
|
532
|
+
// Create, wire and Init() back to back with no failure point in between, so
|
|
533
|
+
// a non-null api_ always means Init() ran and Release() is safe.
|
|
534
|
+
CThostFtdcMdApi *api = CThostFtdcMdApi::CreateFtdcMdApi(flowPath_.c_str());
|
|
535
|
+
if (!api) {
|
|
536
|
+
Napi::Error::New(env, "CreateFtdcMdApi failed").ThrowAsJavaScriptException();
|
|
537
|
+
return env.Undefined();
|
|
538
|
+
}
|
|
539
|
+
api->RegisterSpi(spi_);
|
|
540
|
+
for (auto &addr : fronts_)
|
|
541
|
+
api->RegisterFront(const_cast<char *>(addr.c_str()));
|
|
542
|
+
api->Init();
|
|
543
|
+
api_ = api;
|
|
532
544
|
return env.Undefined();
|
|
533
545
|
}
|
|
534
546
|
|
package/src/native/traderapi.cc
CHANGED
|
@@ -207,7 +207,12 @@ private:
|
|
|
207
207
|
Napi::Value InjectTestLogin(const Napi::CallbackInfo &info);
|
|
208
208
|
int nextArmRequestId();
|
|
209
209
|
|
|
210
|
+
// Non-null only between _start() (CreateFtdcTraderApi + Init) and close():
|
|
211
|
+
// CTP's Release() on an instance that never ran Init() joins a reactor thread
|
|
212
|
+
// that was never started, which crashes on newer glibc (Ubuntu 26.04 / 2.43).
|
|
210
213
|
CThostFtdcTraderApi *api_ = nullptr;
|
|
214
|
+
std::string flowPath_;
|
|
215
|
+
std::vector<std::string> fronts_;
|
|
211
216
|
TraderSpi *spi_ = nullptr;
|
|
212
217
|
EventChannel *ch_ = nullptr;
|
|
213
218
|
RiskEngine risk_;
|
|
@@ -303,12 +308,11 @@ Trader::Trader(const Napi::CallbackInfo &info)
|
|
|
303
308
|
return;
|
|
304
309
|
}
|
|
305
310
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
}
|
|
311
|
+
// The CTP API itself is created in _start(), immediately before Init() (see
|
|
312
|
+
// api_): an instance that is closed or finalized without ever starting then
|
|
313
|
+
// owns no CTP object, so close() never calls Release() on an un-Init()ed API.
|
|
314
|
+
flowPath_ = std::move(flowPath);
|
|
315
|
+
fronts_ = std::move(fronts);
|
|
312
316
|
// Trader uses a lossless staged overflow: order/trade/query returns must never
|
|
313
317
|
// be silently discarded under backpressure.
|
|
314
318
|
ch_ = new EventChannel(4096, static_cast<size_t>(ctpMaxStructSize()),
|
|
@@ -321,13 +325,11 @@ Trader::Trader(const Napi::CallbackInfo &info)
|
|
|
321
325
|
// snapshots, never from a timing window during construction/initial login.
|
|
322
326
|
risk_.markTradeLedgerUncertain();
|
|
323
327
|
risk_.markOrderLedgerUncertain();
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
api_
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
for (auto &addr : fronts)
|
|
330
|
-
api_->RegisterFront(const_cast<char *>(addr.c_str()));
|
|
328
|
+
// Armed triggers fire orders through this Trader. fireArmed() reads api_ on
|
|
329
|
+
// the MD callback thread, but only for arms registered via Arm(), which
|
|
330
|
+
// requires api_ to be set already (on the JS thread, before the arm is
|
|
331
|
+
// published under the registry lock), so the deferred creation adds no race.
|
|
332
|
+
arm_->setSink(this);
|
|
331
333
|
}
|
|
332
334
|
|
|
333
335
|
Trader::~Trader() { doClose(); }
|
|
@@ -1056,7 +1058,7 @@ Napi::Value Trader::ResetPositions(const Napi::CallbackInfo &info) {
|
|
|
1056
1058
|
|
|
1057
1059
|
Napi::Value Trader::Start(const Napi::CallbackInfo &info) {
|
|
1058
1060
|
Napi::Env env = info.Env();
|
|
1059
|
-
if (started_ || !
|
|
1061
|
+
if (started_ || closed_ || !ch_)
|
|
1060
1062
|
return env.Undefined();
|
|
1061
1063
|
napi_status st = ch_->start(env, info[0]);
|
|
1062
1064
|
if (st != napi_ok) {
|
|
@@ -1064,8 +1066,23 @@ Napi::Value Trader::Start(const Napi::CallbackInfo &info) {
|
|
|
1064
1066
|
.ThrowAsJavaScriptException();
|
|
1065
1067
|
return env.Undefined();
|
|
1066
1068
|
}
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
+
started_ = true; // the channel is live; never start it twice, even on failure below
|
|
1070
|
+
// Create, wire and Init() back to back with no failure point in between, so
|
|
1071
|
+
// a non-null api_ always means Init() ran and Release() is safe.
|
|
1072
|
+
CThostFtdcTraderApi *api =
|
|
1073
|
+
CThostFtdcTraderApi::CreateFtdcTraderApi(flowPath_.c_str());
|
|
1074
|
+
if (!api) {
|
|
1075
|
+
Napi::Error::New(env, "CreateFtdcTraderApi failed")
|
|
1076
|
+
.ThrowAsJavaScriptException();
|
|
1077
|
+
return env.Undefined();
|
|
1078
|
+
}
|
|
1079
|
+
api->RegisterSpi(spi_);
|
|
1080
|
+
api->SubscribePublicTopic(THOST_TERT_QUICK);
|
|
1081
|
+
api->SubscribePrivateTopic(THOST_TERT_QUICK);
|
|
1082
|
+
for (auto &addr : fronts_)
|
|
1083
|
+
api->RegisterFront(const_cast<char *>(addr.c_str()));
|
|
1084
|
+
api->Init();
|
|
1085
|
+
api_ = api;
|
|
1069
1086
|
return env.Undefined();
|
|
1070
1087
|
}
|
|
1071
1088
|
|