Search code examples
pythonvisual-studio-codevscode-debugger

VSC debugger python not stopping in breakpoint of class method


I have the following code in python, based on a AWS documentation to consume from a Kinesis stream. However my problem is entirely with VSC, not with anything specific of kinesis.

My code is simple

  1. It imports some libraries
  2. It defines a class, with an init and some method
  3. Outside the class definition, it creates an instance with the __init__ parameters
  4. It calls a method from that instance

I want to enter debug mode in VSC. And specifically I want to enter to the method of the class that is being invoked on step 4. I would expect that when I put a breakpoint in the instance_object.method() and hit Step in it would enter the class's method lines of code so I can go one by one. But instead, it just terminates as if there was nothing to step in to

Here is my specific code:

    import boto3
    import json
    import time
    import datetime
    from botocore.exceptions import ClientError
    
    class Consume():
        def __init__(self,kinesis_client,stream_name):
              self.kinesis_client=kinesis_client
              self.stream_name=stream_name
              self.details=kinesis_client.describe_stream(StreamName='input_stream')
    
        def consuming(self,max_records):
            try:
                response = self.kinesis_client.get_shard_iterator(
                    StreamName=self.stream_name, ShardId=self.details['Shards'][0]['ShardId'],
                    ShardIteratorType='LATEST')
                shard_iter = response['ShardIterator']
                record_count = 0
                while record_count < max_records:
                    response = self.kinesis_client.get_records(
                        ShardIterator=shard_iter, Limit=10)
                    shard_iter = response['NextShardIterator']
                    records = response['Records']
                    print("Got {} records.".format(len(records)))
                    record_count += len(records)
                    yield records
            except ClientError:
                print("Couldn't get records from stream {}.".format(self.stream_name))
                raise

    kinesis_client=boto3.client('kinesis',
                         aws_access_key_id=<some_hidden_string>,
                         aws_secret_access_key=<some_hidden_string>)
    
    consumer=Consume(kinesis_client,stream_name='input_stream')
    consumer.consuming(5)

So, I've set a breakpoint at the last line consumer.consuming(5) and when the debugger hit's that, I click step in but it just terminates directly.

What am I doing wrong? Here is a picture showing my breakpoints

Breakpoints VSC debugging

And this is my launch.json . I am also running this locally on a windows 11 machine in plain VSC on the desktop, no docker container nor anything like that

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

Solution

  • As per rioV8 comment, if the return of a method is a generator, which was in this case due to the usage of yield. The invocation of the method itself won't trigger any code that VSC debugger will execute as runtime. Only until the generator is called will then be runtime code that VSC will enter the method line by line to execute the code.

    In other words, when I added this lines at the end of the code posted in the question

    generator_data_fetch=consumer.consume(5)
    for i in generator_data_fetch:
       print(i)
    

    Then, it will enter the code to debug if a breakpoint is set inside the method, or if a breakpoints is set in for i in generator_data_fetch and then we click Step Into

    In other words, if the return of a method is a generator object, VSC will not enter the method for debugging. Only until the generator is called after calling the method that creates the generator