PTVS icon indicating copy to clipboard operation
PTVS copied to clipboard

Python function with stacked decorators using functools.cache hangs when run without debugging

Open vsfeedback opened this issue 2 years ago • 6 comments

This issue has been moved from a ticket on Developer Community.


I have two Python files. One defines a clock decorator which displays the elapsed time I function took to run.

import time
import functools

def clock(func):
	@functools.wraps(func)
	def clocked(*args, **kwargs):
		t0 = time.perf_counter()
		result = func(*args, **kwargs)
		elapsed = time.perf_counter() - t0
		name = func.__name__
		arg_lst = [repr(arg) for arg in args]
		arg_lst.extend(f'{k}={v!r}' for k, v in kwargs.items())
		arg_str = ', '.join(arg_lst)
		print(f'[{elapsed:0.8f}s] {name}({arg_str}) -> {result!r}')
		return result
	return clocked

@clock
def snooze(seconds):
	time.sleep(seconds)

@clock
def factorial(n):
	return 1 if n < 2 else n*factorial(n-1)

if __name__ == '__main__':
	print('*' * 40, 'Calling snooze(.123)')
	snooze(.123)
	print('*' * 40, 'Calling factorial(6)')
	print('6! = ', factorial(6))

The other file defines a Fibonacci number generator, wrapped with @functools.cache and @clock.

import functools

from clockdeco import clock

@functools.cache
@clock
def fibonacci(n):
	if n < 2:
		return n
	return fibonacci(n - 2) + fibonacci(n - 1)


if __name__ == '__main__':
	print(fibonacci(30))

When the latter file is run with wither Python 3.9.7 or 3.10.6, it will run as expected when run with debugging. However, if run without debugging with a call to fibonacci(28) or greater, the process with hang indefinitely. This does not occur when running the file manually via the Command Prompt.


Original Comments

Feedback Bot on 9/6/2022, 05:32 PM:

(private comment, text removed)


Original Solutions

(no solutions)

vsfeedback avatar Sep 14 '22 13:09 vsfeedback

Hi there, I couldn't repro this. Could you update your Visual Studio to the latest version and see if that still persists? Thanks!

StellaHuang95 avatar Sep 14 '22 17:09 StellaHuang95

@StellaHuang95 I can confirm that this is still occurring with:

  • Microsoft Visual Studio Community 2022 (64-bit) - Version 17.3.4
  • Python 3.10.7 (64-bit) or Python 3.9.7 (64-bit)
  • Windows 11 Pro 21H2 22000.978

I tried deleting the cache files and letting them be recreated, but that didn't seem to help.

JordanBarnartt avatar Sep 17 '22 05:09 JordanBarnartt

@JordanBarnartt I still couldn't repro it on the same version of VS. Could you record your repro steps using ScreenToGif or any other similar screen recorder tool?😊Thank you! repro

StellaHuang95 avatar Sep 21 '22 20:09 StellaHuang95

@StellaHuang95 here you are!

https://user-images.githubusercontent.com/22924314/191904601-e10032f6-4cf9-42d0-ade3-fd0c9426a521.mp4

JordanBarnartt avatar Sep 23 '22 06:09 JordanBarnartt

ok thanks, could you please do me another favor to see if it's a debugpy issue?

  • Set DEBUGPY_LOG_DIR to path where the logs would be generated, run the scripts again and send us the logs. But it needs to be set before you start VS, since there's no setting for that in VS itself. See this link.
  • Check if you can reproduce it in VSCode, with a debug configuration that has "noDebug": true - that should be the equivalent to what you're doing in VS.

StellaHuang95 avatar Sep 23 '22 23:09 StellaHuang95

Please find the logs attached from when the script ran successfully with the debugger (as no logs were generated when run without the debugger). As I was testing this, I also noticed some new behaviour. After running with debugging, I ran without debugging and, to my surprise, the script ran successfully. I repeated this and it ran successfully again, then back to failing. Since, it is switching between successes and failures intermittently, with no discernible pattern (and aren't these just the best kinds of problems). dubugpy_logs.zip

I was not able to reproduce this behaviour in VSCode. I ran the script without debugging a dozen or so times, to make sure it wasn't a fluke.

JordanBarnartt avatar Sep 24 '22 09:09 JordanBarnartt

Hi there, does this still repro in the latest version of VS? I don't see anything in that code that would explain the behavior that you're seeing. This might be interference from anti-malware or something similar. Have you also checked that? Thanks!

StellaHuang95 avatar Feb 23 '23 20:02 StellaHuang95

I'm sorry to report that I can still reproduce the issue. I'm now running:

Microsoft Visual Studio Community 2022
Version 17.5.0
VisualStudio.17.Release/17.5.0+33414.496
Microsoft .NET Framework
Version 4.8.09032

Installed Version: Community

Visual C++ 2022   00482-90000-00000-AA135
Microsoft Visual C++ 2022

ASP.NET and Web Tools   17.5.317.37931
ASP.NET and Web Tools

Azure App Service Tools v3.0.0   17.5.317.37931
Azure App Service Tools v3.0.0

C# Tools   4.5.0-6.23109.5+6a5a63bbc9f4449d9bd1e95a8f9624939c3ccdc3
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter   17.0.22336.2
Provides tools for finding, instantiating and customizing templates in cookiecutter format.

IncrediBuild Build Acceleration   1.6.0.2
IncrediBuild effectively reduces compilation and development times by up to 90%.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

NuGet Package Manager   6.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Python - Django support   17.0.22336.2
Provides templates and integration for the Django web framework.

Python - Profiling support   17.0.22336.2
Profiling support for Python projects.

Python with Pylance   17.0.22336.2
Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

TypeScript Tools   17.0.20105.2003
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.5.0-6.23109.5+6a5a63bbc9f4449d9bd1e95a8f9624939c3ccdc3
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   17.5.0-beta.23053.5+794b7c259d9646a7eb685dad865aa27da7940a21
Microsoft Visual F# Tools

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

My only antivirus software is Windows Defender. The protection history doesn't show anything (the last event was last year).

JordanBarnartt avatar Feb 24 '23 10:02 JordanBarnartt

It still seems to be working for me. The fact that it sometimes doesn't repro for you probably implies some kind of a timing issue, debugging would normally slow down the code, but it's interesting that it happens without debugging. Does this also happen to your other python projects? Have you tried reinstalling or updating your VS?

StellaHuang95 avatar Feb 28 '23 01:02 StellaHuang95

I've not had this issue in any other Python projects, but I've also not had any other Python projects with stacked decorators.

I tried re-installing VS (reboot in-between), but the issue is still occurring as before. This time, I tested with Python 3.11.2 (64-bit).

JordanBarnartt avatar Mar 12 '23 16:03 JordanBarnartt

This issue was closed because it has been stalled for 30 days with no activity. If the issue still persists, please reopen with the information requested. Thanks.

github-actions[bot] avatar Apr 12 '23 00:04 github-actions[bot]