Repository navigation
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 16 minutes. View limit details
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
2665f9c to
8715264
Compare
8715264 to
4f05142
Compare
4f05142 to
c839e4a
Compare
Without a console an app's stdio is /dev/null, so print() and, on devices, NSLog never reach any log stream, and a CLI can't attach to them after launch. In debug builds whose CLI sets forwardStdio in the app's package.json, with stdout still /dev/null, the runtime now pipes them into os_log under the org.nativescript.stdio subsystem. Long lines are split like console messages.
c839e4a to
9aa0902
Compare
NathanWalker
left a comment
There was a problem hiding this comment.
Reviewed this PR's own changes on top of #530 (StdioForwarder.mm). The design looks sound:
- Debug check: "debug" comes from the app's
DEBUGthroughRuntimeConfig.IsDebug, not from the prebuilt framework. - Timing: forwarding starts in
main()beforeUIApplicationMain, andsetvbufcomes beforedup2. LogFormat(): it's warmed up before stdout stops being/dev/null.- Writers: the reader takes no locks, so a slow reader only throttles writers.
Two edge cases are worth fixing before merge. I reproduced both with standalone macOS programs.
1. The reader spins at 100% CPU after EOF (StdioForwarder.mm:44-46). if (count <= 0) return; never cancels the source. If the app replaces or closes fd 1/2 after forwarding starts, the pipe's only write end goes away and the read source fires continuously; I measured ~1.4M handler calls/s, each read() returning 0. That can happen through freopen(path, "a+", stderr) (a common way to save NSLog to a file), dup2(file, 1), or close(1).
Suggested fix: on count == 0, or on count < 0 with errno other than EINTR/EAGAIN, flush pending, then dispatch_source_cancel(source), with a cancel handler that closes readEnd.
2. os_log can feed back into the stderr pipe (:91-95). When OS_ACTIVITY_DT_MODE is set, os_log also writes each entry to fd 2. Once stderr is forwarded, the reader's own os_log_with_type goes back into the pipe it's draining. In my test, one fputs("…\n", stderr) produced ~4,900 growing os_log calls. The reader then blocked writing into its own full pipe, and the next main-thread fputs(stderr) hung.
Xcode sets that variable. An Xcode launch probably attaches a pty and fails the /dev/null check, but that wasn't tested. A cheap guard is to skip forwarding stderr when getenv("OS_ACTIVITY_DT_MODE") is set.
Smaller items:
- 3. A 64 KB flush can cut a UTF-8 character (
:57-59). Step back over continuation bytes, asForEachLogChunkdoes, and keep the tail inpending. - 4. Lines are cut at an embedded NUL (
:52, :58), becauseTNS_WriteToOsLogtakes aconst char*. Astring_viewoverload would fix that and also avoid thesubstr()allocation for every line. - 5. Mac Catalyst.
TARGET_OS_SIMULATORis 0 on Catalyst, where NSLog also reaches the unified log when stderr is a pipe (checked withlog show). So stderr forwarding there would probably print NSLog twice.#if !TARGET_OS_SIMULATOR && !TARGET_OS_MACCATALYSTwould cover it. - 6. Only stdout is checked before stderr is redirected (
:76). If stderr points somewhere real, it gets taken over anyway. AddingIsDevNull(STDERR_FILENO)before forwarding fd 2 would fix it. - 7. The read source is never released. That's only because the target builds without ARC; under ARC, the source would be freed when
Forwardreturns and nothing would be read. A static or a short comment would stop it breaking silently if this file ever moves to ARC. - 8. The commit message for 9aa0902 says non-boolean
forwardStdiovalues are ignored, butisKindOfClass:[NSNumber class]also accepts numbers such as1. That's harmless; either checkCFBooleanGetTypeID()or reword the message.
Question. With --log debug, the printf("Runtime initialization took …") in NativeScript.mm now shows up as a stdout: line on every launch. Is that intended, or should the CLI filter it?
What is the current behavior?
Without a console, an app's stdio is
/dev/null.print()output is lost, and so isNSLogon devices, where it only goes to stderr on iPadOS 26.7. A CLI can't attach to it after launch, because stdio is fixed when the process starts.What is the new behavior?
In debug builds whose CLI sets
forwardStdioin the app'spackage.json, with stdout still/dev/null, the runtime pipes stdout and stderr intoos_logunder theorg.nativescript.stdiosubsystem, withstdoutandstderras categories.print()would wait for a full buffer.NSLogthere also reaches the unified log and would print twice. That meansfputs(..., stderr)isn't forwarded on the simulator.Known limits: output buffered at the moment of a crash is lost, so the last lines before an
abort()may not appear. An unterminated final line isn't forwarded until a newline arrives or it reaches 64 KB.Testing
Demo app with Swift code calling
print()(short, 5,000 characters, background thread),NSLogandfputs(stderr), on both launches of each run:--log debug, simulatorprint()shown, 5,000-character line intact,NSLogonce (fromFoundation)--log debug, iPad (iPadOS 26.7)print(),NSLogand stderr all shown oncesimctl launch --console-ptyprint()goes to the console directlyOlder iOS versions weren't tested, and
NSLogmay behave differently there. A launch from Xcode itself wasn't tested either.Part of a stack
Base: #530. The CLI side is NativeScript/nativescript-cli#6181.
Summary by CodeRabbit