Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
kmeshctl to use RunE and eliminate os.Exit calls for MCP safety
Codecov Report✅ All modified and coverable lines are covered by tests. Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
f8183fd to
449e9c7
Compare
c9ce290 to
8b6b657
Compare
642f3ff to
e149f2d
Compare
| return fmt.Errorf("failed to create cli client: %v", err) | ||
| } | ||
|
|
||
| fw, err := utils.CreateKmeshPortForwarder(cli, podName) |
There was a problem hiding this comment.
Missing fw.Close()
76106a9 to
df89c07
Compare
df89c07 to
538a51e
Compare
538a51e to
6b3f0b0
Compare
6b3f0b0 to
110e6f5
Compare
110e6f5 to
99621dc
Compare
99621dc to
f669c55
Compare
f669c55 to
664b0b6
Compare
664b0b6 to
f8ebbdd
Compare
Signed-off-by: devGPP23 <gauravpatil232005@gmail.com>
f8ebbdd to
08150f8
Compare
|
Hi @LiZhenCheng9527 1 ) Added defer fw.Close() across all ctl/ port-forwarder helpers to prevent any connection/goroutine leaks. |
Problem
I noticed any network error triggers a hard
os.Exit(1)deep inside the Kmesh codebase. This abruptly crashes the entire MCP server process instead of gracefully returning the error to the caller.Before this fix, an error fetching pods would print a raw logger message and forcefully terminate the CLI process without passing the error back to the Cobra framework:
Solution
To ensure
kmeshctlis completely safe to embed in long-running processes, I audited the remaining subcommands (authz,version,secret,monitoring) and performed a full migration.Specifically, I did the following:
1)Migrated the
Runfield toRunEin all subcommands so Cobra can natively handle returned errors.2)Systematically removed all
os.Exit(1)calls.3)Refactored helper functions to return standard Go error objects rather than logging and crashing the process.
4)Replaced the hard dependency on standard
stdout/stderrby usingio.Writerandcmd.OutOrStdout()where applicable.How it Improves
After my changes, errors are gracefully propagated up through
fmt.Errorfrather than forcefully killing the application.we can safely import these CLI functions without the risk of an unexpected fatal exit.In the CLI itself, Cobra now successfully catches the returned error and formats it cleanly with an Error: prefix instead of abruptly terminating:
Now Terminal Output is like below with Error: prefic
This makes
kmeshctlmuch safer, more idiomatic, and fully embeddable!