-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_infinite_loop.py
More file actions
314 lines (242 loc) · 9.85 KB
/
debug_infinite_loop.py
File metadata and controls
314 lines (242 loc) · 9.85 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
"""
Debug script to trace infinite loop in Microsoft MCP server.
This script will run the email_operations function with extensive logging
to identify exactly where the code is hanging.
"""
import logging
import os
import signal
import sys
import traceback
from contextlib import contextmanager
# Add the source directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
# Set up stderr-only logging as per Python mastery guidelines
logging.basicConfig(
level=logging.DEBUG,
handlers=[logging.StreamHandler(sys.stderr)],
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
log = logging.getLogger(__name__)
# Timeout handler to prevent infinite hanging
@contextmanager
def timeout_context(seconds):
"""Context manager to timeout operations."""
def timeout_handler(signum, frame):
raise TimeoutError(f"Operation timed out after {seconds} seconds")
# Set the signal handler
old_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(seconds)
try:
yield
finally:
# Restore the old handler
signal.alarm(0)
signal.signal(signal.SIGALRM, old_handler)
def debug_trace_function_calls():
"""Set up function call tracing to see where code hangs."""
def trace_calls(frame, event, arg):
if event == "call":
filename = frame.f_code.co_filename
if "microsoft_mcp" in filename:
func_name = frame.f_code.co_name
line_no = frame.f_lineno
log.debug(f"CALL: {filename}:{line_no} in {func_name}")
elif event == "return":
filename = frame.f_code.co_filename
if "microsoft_mcp" in filename:
func_name = frame.f_code.co_name
log.debug(f"RETURN: {func_name}")
return trace_calls
sys.settrace(trace_calls)
def test_auth_first():
"""Test authentication functionality first."""
log.info("=== TESTING AUTH OPERATIONS ===")
try:
from microsoft_mcp.auth_tool import auth_operations
log.info("Calling auth_operations with action='status'")
with timeout_context(10):
result = auth_operations(action="status")
log.info(f"Auth status result: {result}")
return result
except Exception as e:
log.error(f"Auth test failed: {e}")
log.error(traceback.format_exc())
return None
def test_token_retrieval():
"""Test token retrieval specifically."""
log.info("=== TESTING TOKEN RETRIEVAL ===")
try:
from microsoft_mcp.auth import get_token
from microsoft_mcp.auth import list_accounts
log.info("Getting accounts list")
accounts = list_accounts()
log.info(f"Found accounts: {[acc.username for acc in accounts]}")
if accounts:
account_id = accounts[0].account_id
log.info(f"Getting token for account: {account_id}")
with timeout_context(10):
token = get_token(account_id)
log.info(f"Token retrieved successfully (length: {len(token)})")
return {"status": "success", "token_length": len(token)}
log.warning("No accounts found")
return {"status": "error", "message": "No accounts found"}
except Exception as e:
log.error(f"Token retrieval failed: {e}")
log.error(traceback.format_exc())
return None
def test_graph_request():
"""Test basic graph API request."""
log.info("=== TESTING GRAPH API REQUEST ===")
try:
from microsoft_mcp import graph
from microsoft_mcp.auth import list_accounts
accounts = list_accounts()
if not accounts:
log.error("No accounts available for testing")
return None
account_id = accounts[0].account_id
log.info(f"Testing graph request with account: {account_id}")
# Test a simple graph request
log.info("Making simple graph request to /me")
with timeout_context(15):
result = graph.request("GET", "/me", account_id)
log.info(f"Graph request successful: {result.get('displayName', 'Unknown') if result else 'No result'}")
return {"status": "success", "user": result.get("displayName") if result else None}
except Exception as e:
log.error(f"Graph request failed: {e}")
log.error(traceback.format_exc())
return None
def test_email_list_minimal():
"""Test minimal email listing functionality."""
log.info("=== TESTING MINIMAL EMAIL LIST ===")
try:
from microsoft_mcp import graph
from microsoft_mcp.auth import list_accounts
accounts = list_accounts()
if not accounts:
log.error("No accounts available for testing")
return None
account_id = accounts[0].account_id
log.info(f"Testing email list with account: {account_id}")
# Test email listing with minimal parameters
endpoint = "/me/mailFolders/inbox/messages"
params = {
"$top": 1, # Only get 1 email to minimize data
"$select": "id,subject", # Minimal fields
}
log.info(f"Making request to {endpoint} with params: {params}")
with timeout_context(20):
result = graph.request("GET", endpoint, account_id, params=params)
if result and "value" in result:
log.info(f"Email list successful: {len(result['value'])} emails")
return {"status": "success", "email_count": len(result["value"])}
log.warning("No emails or unexpected response format")
return {"status": "warning", "message": "No emails found or unexpected response"}
except Exception as e:
log.error(f"Email list failed: {e}")
log.error(traceback.format_exc())
return None
def test_pagination_function():
"""Test the pagination function specifically."""
log.info("=== TESTING PAGINATION FUNCTION ===")
try:
from microsoft_mcp import graph
from microsoft_mcp.auth import list_accounts
accounts = list_accounts()
if not accounts:
log.error("No accounts available for testing")
return None
account_id = accounts[0].account_id
log.info(f"Testing pagination with account: {account_id}")
# Test pagination directly
endpoint = "/me/mailFolders/inbox/messages"
params = {
"$top": 1,
"$select": "id,subject",
}
log.info("Testing graph.paginate function")
with timeout_context(20):
# Convert iterator to list to see if it hangs
items = list(graph.paginate(endpoint, account_id, params=params, limit=1))
log.info(f"Pagination successful: {len(items)} items")
return {"status": "success", "items_count": len(items)}
except Exception as e:
log.error(f"Pagination test failed: {e}")
log.error(traceback.format_exc())
return None
def test_email_operations_function():
"""Test the actual email_operations function that's hanging."""
log.info("=== TESTING EMAIL_OPERATIONS FUNCTION ===")
try:
from microsoft_mcp.auth import list_accounts
from microsoft_mcp.email_tool import email_operations
accounts = list_accounts()
if not accounts:
log.error("No accounts available for testing")
return None
account_id = accounts[0].account_id
log.info(f"Testing email_operations with account: {account_id}")
log.info("Calling email_operations with action='list', limit=1")
with timeout_context(30):
result = email_operations(
account_id=account_id,
action="list",
limit=1,
include_body=False # Minimize data transfer
)
log.info(f"Email operations successful: {result}")
return result
except Exception as e:
log.error(f"Email operations failed: {e}")
log.error(traceback.format_exc())
return None
def main():
"""Main debug function."""
log.info("Starting Microsoft MCP infinite loop debugging")
log.info("=" * 60)
# Enable function call tracing
debug_trace_function_calls()
results = {}
# Test sequence from basic to complex
tests = [
("auth_status", test_auth_first),
("token_retrieval", test_token_retrieval),
("graph_request", test_graph_request),
("email_list_minimal", test_email_list_minimal),
("pagination_function", test_pagination_function),
("email_operations", test_email_operations_function),
]
for test_name, test_func in tests:
log.info(f"\n--- Running test: {test_name} ---")
try:
result = test_func()
results[test_name] = result
if result and result.get("status") == "error":
log.warning(f"Test {test_name} returned error, stopping here")
break
except TimeoutError as e:
log.error(f"Test {test_name} TIMED OUT: {e}")
results[test_name] = {"status": "timeout", "message": str(e)}
break
except Exception as e:
log.error(f"Test {test_name} CRASHED: {e}")
results[test_name] = {"status": "crash", "message": str(e)}
break
# Disable tracing
sys.settrace(None)
log.info("\n" + "=" * 60)
log.info("DEBUG RESULTS SUMMARY:")
for test_name, result in results.items():
if result:
status = result.get("status", "unknown")
log.info(f" {test_name}: {status}")
else:
log.info(f" {test_name}: None (failed)")
# Print results to stdout for easy reading
print("\n=== MICROSOFT MCP DEBUG RESULTS ===", file=sys.stdout)
for test_name, result in results.items():
print(f"{test_name}: {result}", file=sys.stdout)
if __name__ == "__main__":
main()