Commit e76a4d2
fix(egg): reopen logger streams on snapshot restore (#6001)
## Motivation
After a V8 startup snapshot is restored, writing logs raised `... log
stream had been closed` (e.g. `egg-schedule.log` when the schedule
plugin's `didReady` starts).
Root cause: `EggApplicationCore.snapshotWillSerialize` closed every
logger **and** discarded the `EggLoggers` instance (`#loggers =
undefined`), relying on the lazy `loggers` getter to rebuild a *fresh*
`EggLoggers` on restore. But plugins such as `@eggjs/schedule` capture
an individual logger reference in their boot-hook **constructor** during
the load phase (before serialize). The fresh `EggLoggers` left those
captured references pointing at the original **closed** `FileTransport`,
so writing through them after restore threw.
## Scope
`packages/egg` snapshot lifecycle resource rebuild only (no bundler
changes). Preserve logger **identity** across the snapshot instead of
rebuilding:
- `snapshotWillSerialize`: still `logger.close()` each logger (releases
the fd and clears the `FileBufferTransport` flush interval) but
**keeps** the `EggLoggers` instance.
- `snapshotDidDeserialize`: new `#reopenLoggers()` reopens the *same*
logger objects in place — `transport.reload()` reopens each
`FileTransport` stream, and the `FileBufferTransport` flush interval is
restarted (which `close()` clears but `reload()` does not restore). This
keeps the `willSerialize` / `didDeserialize` resource pairing complete
and also fixes the parallel `onelogger` global-registry staleness.
Messenger (recreated) and the `unhandledRejection` listener
(re-attached) were already paired; the agent keepalive timer is
recreated by the lifecycle resume — no changes needed there.
## Test evidence
`packages/egg/test/snapshot.test.ts`:
- New regression test emulates a plugin capturing a logger before the
snapshot, then asserts the file transport goes writable → closed →
writable across serialize/deserialize, that logger **identity is
preserved**, that the buffer flush timer is restarted, and that a log
written through the captured reference after restore actually reaches
the `-web.log` file (poll-read to avoid racing the async stream flush).
- Updated the previous "clean up loggers" test to assert instance
preservation.
`pnpm --filter=egg run test test/snapshot.test.ts` → 11/11 pass. `tsgo
--noEmit` clean. (Pre-existing unrelated failures in
`test/lib/core/logger.test.ts` are present on `next` without this
change.)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved snapshot restore behavior so logger output continues working
after a snapshot is serialized and reloaded.
* Preserved existing logger instances across restore to avoid missed or
interrupted log writes.
* Ensured buffered log transports resume flushing correctly after
restore, including continued writing to disk.
* **Tests**
* Updated snapshot lifecycle tests to verify logger preservation and
transport reopening/flush behavior after restore.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent a311fee commit e76a4d2
2 files changed
Lines changed: 122 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
524 | 524 | | |
525 | 525 | | |
526 | 526 | | |
527 | | - | |
528 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
529 | 537 | | |
530 | 538 | | |
531 | 539 | | |
532 | 540 | | |
533 | 541 | | |
| 542 | + | |
| 543 | + | |
534 | 544 | | |
535 | 545 | | |
536 | | - | |
537 | 546 | | |
538 | 547 | | |
539 | 548 | | |
540 | 549 | | |
541 | 550 | | |
542 | 551 | | |
543 | | - | |
544 | | - | |
545 | | - | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
546 | 555 | | |
547 | 556 | | |
548 | 557 | | |
549 | 558 | | |
550 | 559 | | |
551 | 560 | | |
| 561 | + | |
552 | 562 | | |
553 | 563 | | |
554 | 564 | | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
555 | 592 | | |
556 | 593 | | |
557 | 594 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
122 | | - | |
| 123 | + | |
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
| |||
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
131 | | - | |
132 | | - | |
| 132 | + | |
| 133 | + | |
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
144 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
145 | 215 | | |
146 | 216 | | |
147 | 217 | | |
| |||
0 commit comments