chrismitchellonline

Debugging Port Issues with SAM and Visual Studio Code

2021-12-09

When using SAM to debug Lambda functions locally with Visual Studio Code the debugger can be helpful to trace variables and step through code. To do so Visual Studio Code needs to connect to a debugger on port 5858. In this article we’ll discuss how to debug connection issues on this port.

Errors Connecting to Debugger

When trying to connect to the debugger in Visual Studio Code you might see a long error output, ending in something similar to this:

docker.errors.APIError: 500 Server Error: Internal Server Error ("driver failed programming external connectivity on endpoint elated_vaughan (0d0037091d4900a9349ee68e5a17ba8c9ea41050b192bba6e0489137342bfc97): Bind for 127.0.0.1:5858 failed: port is already allocated")

Which means the port 5858 is in use by another application.

Finding Port Usage (Mac)

To find out information about what application is listening to port 5858, run the following command:

lsof -i:5858

I see the following output:

com.docke 901 cmitche4   68u  IPv4 0x13a0affb4c34949d      0t0  TCP localhost:5858 (LISTEN)

It seems as though Docker is running a container listening to por 5858

Managing Docker Containers

Docker is used by SAM to run Lambda functions and connect to Visual Studio Code debugger. In this case a container must have failed to stop after a session.

To view running docker containers, run the following command:

docker ps

I see the following output:

e6b2cbd4f554   amazon/aws-sam-cli-emulation-image-nodejs12.x:rapid-1.27.2   "/var/rapid/aws-lamb…"   17 minutes ago   Up 17 minutes   127.0.0.1:5858->5858/tcp, 127.0.0.1:6985->8080/tcp   zealous_lamport

From the output I can see the container is in fact listening on port 5858. To kill this container, run the following command, where container ID is the first string in the previous output:

docker stop e6b2cbd4f554

And I found that it helps clean up space and containers by running a docker system prune command as well:

docker system prune

Verifying the Port

Now that the container is removed, verify that nothing is listening on port 5858:

lsof -i:5858

The output should be empty.

Conclusion

With no applications listening on port 5858, you should now be able to run the debugger in VS Code. Have you had other issues connecting to a debugger in VS Code? Leave a comment below and we can help debug your issue. Happy coding!

comments powered by Disqus
Social Media
Sponsor