-
Notifications
You must be signed in to change notification settings - Fork 0
Http improvements #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Http improvements #108
Changes from all commits
2958bfc
dec7be0
6aa4238
55a5f87
5122d8b
0413c45
3749d4d
bc8f451
784dc8c
85932f6
9b6e198
3d3fa8e
d5eabc6
275093a
8dae8e2
02902f7
1794038
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,24 @@ public async Task<bool> StartAsync() | |
| var mode = ResolvePreferredMode(); | ||
| try | ||
| { | ||
| // Treat transports as mutually exclusive for user-driven session starts: | ||
| // stop the *other* transport first to avoid duplicated sessions (e.g. stdio lingering when switching to HTTP). | ||
| var otherMode = mode == TransportMode.Http ? TransportMode.Stdio : TransportMode.Http; | ||
| try | ||
| { | ||
| await _transportManager.StopAsync(otherMode); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| McpLog.Warn($"Error stopping other transport ({otherMode}) before start: {ex.Message}"); | ||
| } | ||
|
|
||
| // Legacy safety: stdio may have been started outside TransportManager state. | ||
| if (otherMode == TransportMode.Stdio) | ||
| { | ||
| try { StdioBridgeHost.Stop(); } catch { } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Empty catch block silently swallows all exceptions. Consider logging at debug level or checking for specific exception types. Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! Prompt To Fix With AIThis is a comment left during a code review.
Path: MCPForUnity/Editor/Services/BridgeControlService.cs
Line: 100:100
Comment:
**style:** Empty catch block silently swallows all exceptions. Consider logging at debug level or checking for specific exception types.
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise. |
||
| } | ||
|
|
||
| bool started = await _transportManager.StartAsync(mode); | ||
| if (!started) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using MCPForUnity.Editor.Constants; | ||
| using MCPForUnity.Editor.Helpers; | ||
| using MCPForUnity.Editor.Services.Transport; | ||
| using UnityEditor; | ||
|
|
||
| namespace MCPForUnity.Editor.Services | ||
| { | ||
| /// <summary> | ||
| /// Best-effort cleanup when the Unity Editor is quitting. | ||
| /// - Stops active transports so clients don't see a "hung" session longer than necessary. | ||
| /// - If HTTP Local is selected, attempts to stop the local HTTP server (guarded by PID heuristics). | ||
| /// </summary> | ||
| [InitializeOnLoad] | ||
| internal static class McpEditorShutdownCleanup | ||
| { | ||
| static McpEditorShutdownCleanup() | ||
| { | ||
| // Guard against duplicate subscriptions across domain reloads. | ||
| try { EditorApplication.quitting -= OnEditorQuitting; } catch { } | ||
| EditorApplication.quitting += OnEditorQuitting; | ||
| } | ||
|
|
||
| private static void OnEditorQuitting() | ||
| { | ||
| // 1) Stop transports (best-effort, bounded wait). | ||
| try | ||
| { | ||
| var transport = MCPServiceLocator.TransportManager; | ||
|
|
||
| Task stopHttp = transport.StopAsync(TransportMode.Http); | ||
| Task stopStdio = transport.StopAsync(TransportMode.Stdio); | ||
|
|
||
| try { Task.WaitAll(new[] { stopHttp, stopStdio }, 750); } catch { } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Avoid hard failures on quit. | ||
| McpLog.Warn($"Shutdown cleanup: failed to stop transports: {ex.Message}"); | ||
| } | ||
|
|
||
| // 2) Stop local HTTP server if it was Unity-managed (best-effort). | ||
| try | ||
| { | ||
| bool useHttp = EditorPrefs.GetBool(EditorPrefKeys.UseHttpTransport, true); | ||
| string scope = string.Empty; | ||
| try { scope = EditorPrefs.GetString(EditorPrefKeys.HttpTransportScope, string.Empty); } catch { } | ||
|
|
||
| bool stopped = false; | ||
| bool httpLocalSelected = | ||
| useHttp && | ||
| (string.Equals(scope, "local", StringComparison.OrdinalIgnoreCase) | ||
| || (string.IsNullOrEmpty(scope) && MCPServiceLocator.Server.IsLocalUrl())); | ||
|
|
||
| if (httpLocalSelected) | ||
| { | ||
| // StopLocalHttpServer is already guarded to only terminate processes that look like mcp-for-unity. | ||
| // If it refuses to stop (e.g. URL was edited away from local), fall back to the Unity-managed stop. | ||
| stopped = MCPServiceLocator.Server.StopLocalHttpServer(); | ||
| } | ||
|
|
||
| // Always attempt to stop a Unity-managed server if one exists. | ||
| // This covers cases where the user switched transports (e.g. to stdio) or StopLocalHttpServer refused. | ||
| if (!stopped) | ||
| { | ||
| MCPServiceLocator.Server.StopManagedLocalHttpServer(); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| McpLog.Warn($"Shutdown cleanup: failed to stop local HTTP server: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Uses string.Contains() to parse CLI output format - this could be fragile if the CLI output format changes
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI