File size: 7,938 Bytes
e75e43f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
diff --git a/cli/azd/internal/vsrpc/handler_test.go b/cli/azd/internal/vsrpc/handler_test.go
index a4f33297f..9d78cc34d 100644
--- a/cli/azd/internal/vsrpc/handler_test.go
+++ b/cli/azd/internal/vsrpc/handler_test.go
@@ -3,6 +3,7 @@ package vsrpc
 import (
 	"context"
 	"errors"
+	"strings"
 	"testing"
 
 	"github.com/stretchr/testify/require"
@@ -83,6 +84,144 @@ func TestHandler(t *testing.T) {
 	t.Run("HandlerFunc3", func(t *testing.T) { runHandlerSuite(t, newHandlerFunc3, []any{"arg0", "arg1", "arg2"}, "ok") })
 }
 
+type panicHandlerCase struct {
+	name    string
+	message string
+	handler Handler
+	request jsonrpc2.Request
+}
+
+func TestPanicRecoveredAcrossHandlerAdapters(t *testing.T) {
+	cases := []panicHandlerCase{
+		{
+			name:    "action zero args",
+			message: "hidden action zero panic",
+			handler: HandlerAction0(func(context.Context) error {
+				panicRpcTestFrame("hidden action zero panic")
+				return nil
+			}),
+			request: requestWithParams(t),
+		},
+		{
+			name:    "action one arg",
+			message: "hidden action one panic",
+			handler: HandlerAction1(func(ctx context.Context, value string) error {
+				require.Equal(t, "alpha", value)
+				panicRpcTestFrame("hidden action one panic")
+				return nil
+			}),
+			request: requestWithParams(t, "alpha"),
+		},
+		{
+			name:    "action two args",
+			message: "hidden action two panic",
+			handler: HandlerAction2(func(ctx context.Context, value string, count int) error {
+				require.Equal(t, "bravo", value)
+				require.Equal(t, 42, count)
+				panicRpcTestFrame("hidden action two panic")
+				return nil
+			}),
+			request: requestWithParams(t, "bravo", 42),
+		},
+		{
+			name:    "action three args",
+			message: "hidden action three panic",
+			handler: HandlerAction3(func(ctx context.Context, value string, count int, flag bool) error {
+				require.Equal(t, "charlie", value)
+				require.Equal(t, 7, count)
+				require.True(t, flag)
+				panicRpcTestFrame("hidden action three panic")
+				return nil
+			}),
+			request: requestWithParams(t, "charlie", 7, true),
+		},
+		{
+			name:    "func zero args",
+			message: "hidden func zero panic",
+			handler: HandlerFunc0(func(context.Context) (string, error) {
+				panicRpcTestFrame("hidden func zero panic")
+				return "", nil
+			}),
+			request: requestWithParams(t),
+		},
+		{
+			name:    "func one arg",
+			message: "hidden func one panic",
+			handler: HandlerFunc1(func(ctx context.Context, value string) (string, error) {
+				require.Equal(t, "delta", value)
+				panicRpcTestFrame("hidden func one panic")
+				return "", nil
+			}),
+			request: requestWithParams(t, "delta"),
+		},
+		{
+			name:    "func two args",
+			message: "hidden func two panic",
+			handler: HandlerFunc2(func(ctx context.Context, value string, count int) (string, error) {
+				require.Equal(t, "echo", value)
+				require.Equal(t, 84, count)
+				panicRpcTestFrame("hidden func two panic")
+				return "", nil
+			}),
+			request: requestWithParams(t, "echo", 84),
+		},
+		{
+			name:    "func three args",
+			message: "hidden func three panic",
+			handler: HandlerFunc3(func(ctx context.Context, value string, count int, flag bool) (string, error) {
+				require.Equal(t, "foxtrot", value)
+				require.Equal(t, 21, count)
+				require.False(t, flag)
+				panicRpcTestFrame("hidden func three panic")
+				return "", nil
+			}),
+			request: requestWithParams(t, "foxtrot", 21, false),
+		},
+	}
+
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			var repliedErr error
+			reply := func(ctx context.Context, result any, err error) error {
+				repliedErr = err
+				return err
+			}
+
+			var handlerErr error
+			require.NotPanics(t, func() {
+				handlerErr = tc.handler(context.Background(), nil, reply, tc.request)
+			})
+
+			require.Error(t, handlerErr)
+			require.Same(t, repliedErr, handlerErr)
+			assertPanicRpcError(t, handlerErr, tc.message)
+		})
+	}
+}
+
+func requestWithParams(t *testing.T, params ...any) jsonrpc2.Request {
+	t.Helper()
+
+	req, err := jsonrpc2.NewCall(jsonrpc2.NewNumberID(1), "hidden/test", params)
+	require.NoError(t, err)
+
+	return req
+}
+
+func assertPanicRpcError(t *testing.T, err error, panicMessage string) {
+	t.Helper()
+
+	var rpcErr *jsonrpc2.Error
+	require.True(t, errors.As(err, &rpcErr), "expected JSON-RPC error, got %T: %v", err, err)
+	require.Equal(t, jsonrpc2.InternalError, rpcErr.Code)
+	require.True(t, strings.HasPrefix(rpcErr.Message, "panic:"), "message should start with panic:, got %q", rpcErr.Message)
+	require.Contains(t, rpcErr.Message, panicMessage)
+}
+
+func panicRpcTestFrame(message string) {
+	panic(message)
+}
+
 func newHandlerAction0(t *testing.T, tc handlerTestCase) Handler {
 	return HandlerAction0(func(ctx context.Context) error {
 		select {
diff --git a/cli/azd/internal/vsrpc/server_test.go b/cli/azd/internal/vsrpc/server_test.go
index 4ed8aad0e..3f5a3016c 100644
--- a/cli/azd/internal/vsrpc/server_test.go
+++ b/cli/azd/internal/vsrpc/server_test.go
@@ -6,8 +6,10 @@ import (
 	"errors"
 	"net/http/httptest"
 	"net/url"
+	"strings"
 	"sync"
 	"testing"
+	"time"
 
 	"github.com/gorilla/websocket"
 	"github.com/stretchr/testify/require"
@@ -132,3 +134,64 @@ func TestObserverable(t *testing.T) {
 	// The onCompleted message takes no parameters and the args value is empty.
 	require.Len(t, onCompletedParams[0], 0)
 }
+
+func TestDebugServicePanicErrorDetails(t *testing.T) {
+	rpcConn := debugRpcConn(t)
+
+	err := callTestPanicAsync(t, rpcConn, "panic probe alpha hidden")
+	assertDebugPanicError(t, err, "panic probe alpha hidden")
+}
+
+func TestDebugServicePanicDoesNotCloseConnection(t *testing.T) {
+	rpcConn := debugRpcConn(t)
+
+	firstErr := callTestPanicAsync(t, rpcConn, "panic probe beta one hidden")
+	assertDebugPanicError(t, firstErr, "panic probe beta one hidden")
+
+	secondErr := callTestPanicAsync(t, rpcConn, "panic probe beta two hidden")
+	assertDebugPanicError(t, secondErr, "panic probe beta two hidden")
+}
+
+func debugRpcConn(t *testing.T) jsonrpc2.Conn {
+	t.Helper()
+
+	debugServer := httptest.NewServer(newDebugService())
+	t.Cleanup(debugServer.Close)
+
+	serverUrl, err := url.Parse(debugServer.URL)
+	require.NoError(t, err)
+	serverUrl.Scheme = "ws"
+
+	wsConn, _, err := websocket.DefaultDialer.Dial(serverUrl.String(), nil)
+	require.NoError(t, err)
+	t.Cleanup(func() {
+		require.NoError(t, wsConn.Close())
+	})
+
+	rpcConn := jsonrpc2.NewConn(newWebSocketStream(wsConn))
+	rpcConn.Go(context.Background(), nil)
+
+	return rpcConn
+}
+
+func callTestPanicAsync(t *testing.T, rpcConn jsonrpc2.Conn, message string) error {
+	t.Helper()
+
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+	defer cancel()
+
+	_, err := rpcConn.Call(ctx, "TestPanicAsync", []any{message}, nil)
+	require.Error(t, err)
+
+	return err
+}
+
+func assertDebugPanicError(t *testing.T, err error, panicMessage string) {
+	t.Helper()
+
+	var rpcErr *jsonrpc2.Error
+	require.True(t, errors.As(err, &rpcErr), "expected JSON-RPC error, got %T: %v", err, err)
+	require.Equal(t, jsonrpc2.InternalError, rpcErr.Code)
+	require.True(t, strings.HasPrefix(rpcErr.Message, "panic:"), "message should start with panic:, got %q", rpcErr.Message)
+	require.Contains(t, rpcErr.Message, panicMessage)
+}
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+set -uo pipefail
+
+cd /testbed
+
+case "${1:-}" in
+  base)
+    go test github.com/azure/azure-dev/cli/azd/internal/vsrpc -v -count=1 -run '^(TestHandler|TestCancellation|TestObserverable)$'
+    ;;
+  new)
+    go test github.com/azure/azure-dev/cli/azd/internal/vsrpc -v -count=1 -run '^(TestPanicRecoveredAcrossHandlerAdapters|TestDebugServicePanicErrorDetails|TestDebugServicePanicDoesNotCloseConnection)$'
+    ;;
+  *)
+    echo "usage: $0 base|new"
+    exit 2
+    ;;
+esac