ConnectionResetError: Errno 104 Connection reset by peer
  • 02-May-2023
Lightrun Team
Author Lightrun Team
ConnectionResetError: Errno 104 Connection reset by peer

ConnectionResetError: [Errno 104] Connection reset by peer

Lightrun Team
Lightrun Team

Explanation of the problem

The user is encountering an error while using Tornado and aioredis in their project. The error message shows a traceback of the error and the versions of the libraries being used – aioredis==1.2.0 and tornado==5.1.1. The error seems to be related to a connection being reset by the peer. The traceback shows that the error occurred in the models/ file when executing a get command on a Redis key using aioredis library. The error is repeated multiple times before leading to the ConnectionResetError.

One possible cause of this error is a dropped connection between the client and Redis server. This could happen if the Redis server crashes, the network connection is lost, or the Redis server is restarted. Another possible cause could be a misconfiguration of the Redis server or the client, such as using the wrong port or authentication credentials.

To solve this issue, the user should first check if the Redis server is running and accessible from the client. They can do this by using the Redis command-line tool or a Redis client library. If the Redis server is running and accessible, they should check the configuration of the Redis client, especially the connection parameters such as the host, port, and authentication credentials. The user could also try upgrading the aioredis library to the latest version, as newer versions might have fixed some bugs or added features that could help with this issue.

In addition, the user can increase the timeout for Redis connections to avoid the ConnectionResetError. They can do this by setting the socket_timeout option to a higher value when creating the Redis client. Another solution is to use a Redis connection pool to manage connections to the Redis server. This can help avoid creating and destroying connections for each request, which can be expensive and lead to dropped connections.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for ConnectionResetError: [Errno 104] Connection reset by peer

The error seems to be related to a TCP connection reset by peer. One possible solution is to use the tcpkill utility from dsniff, which is available on both Linux and macOS. This utility can listen to a specific port and reset any TCP connection that comes through it. To use it, you need to launch tcpkill and specify the port to listen to, and force an IPv4 connection. After this, any connection to that port will be reset, which can be helpful to diagnose the error.

Another answer suggests that the issue may be related to the version of Aioredis being used. With the release of redis-py 4.2.0rc1, the expectation is that this issue will go away once migrated to redis. However, it is not clear when a non-rc version of redis-py will be released. This could be a potential solution, depending on the root cause of the issue.

In summary, the two proposed solutions are to use the tcpkill utility to reset TCP connections or to migrate to redis-py 4.2.0rc1 or higher. It is important to note that the underlying cause of the issue is not clear, so it may require further investigation to determine the most appropriate solution.

Other popular problems with Aioredis-py

Problem: Connection reset errors when using aioredis-py with macOS

One of the most common issues encountered when using aioredis-py with macOS is connection reset errors. This error occurs when the TCP connection to the Redis server is reset unexpectedly, resulting in data loss and potential data corruption


Unfortunately, there is no built-in solution for this problem in macOS, but there are workarounds that can be employed. One such workaround is to use the tcpkill utility from dsniff, which can reset TCP connections. By launching tcpkill and specifying the port number of the Redis server, the utility will listen to the loopback interface and reset any TCP connection to that port. To avoid the issue of IPv6, users should force an IPv4 connection by specifying the Redis host as “”.


Problem: Timeouts when using aioredis-py with Redis clusters

Another common issue that users face when using aioredis-py is timeouts when working with Redis clusters. This can occur when a node in the cluster becomes unavailable or when there is a network partition. In such cases, the aioredis-py client may not be able to connect to the Redis cluster and will eventually time out.


One way to address this issue is to increase the timeout value in the aioredis-py client to a suitable value, depending on the network conditions and the size of the Redis cluster. Another option is to use a load balancer to route requests to available nodes in the cluster, thus reducing the chances of timeouts.

Problem: Issues with Redis Sentinel support in aioredis-py

Redis Sentinel is a popular high-availability solution for Redis, but aioredis-py has limited support for it. The main issue with Redis Sentinel support in aioredis-py is that it does not support automatic failover. This means that if the master node in the Redis Sentinel setup goes down, the client will not automatically switch to the new master node.


To address this issue, users can implement a custom failover mechanism by monitoring the Redis Sentinel setup and switching to the new master node when the old one goes down. Alternatively, users can consider using other Redis clients that have better support for Redis Sentinel, such as redis-py.

A brief introduction to Aioredis-py

aioredis-py is a Python package that provides an asynchronous Redis client library built on top of asyncio library. It aims to provide an easy-to-use and efficient interface to interact with Redis servers from Python applications that use asyncio. The package supports Redis features such as Pub/Sub, Lua scripting, transactions, and pipelining.

The package provides a simple and intuitive API to connect to Redis servers and execute Redis commands. It provides support for both Redis standalone and Redis cluster modes, allowing developers to scale their Redis clusters easily. Additionally, aioredis-py supports connection pooling and connection sharing, which allows multiple Redis clients to share a single connection pool, improving the performance of the Redis client. The package also supports pipelining, which allows multiple Redis commands to be sent to the server in a single batch, reducing network round trips and improving performance. Overall, aioredis-py provides an efficient and user-friendly interface for interacting with Redis servers from asyncio-based Python applications.

Most popular use cases for Aioredis-py

1. aioredis-py can be used to connect and communicate with Redis servers in an asyncio environment. It provides an easy-to-use, high-level API for executing Redis commands asynchronously. For instance, to execute a GET command in Redis using aioredis-py, you can do the following:

import asyncio
import aioredis

async def main():
    redis = await aioredis.create_redis_pool('redis://localhost')
    result = await redis.get('my_key')
    await redis.wait_closed()

This will create a connection pool to a Redis server running on localhost, execute the GET command on the ‘my_key’ key, and print the result.


2. aioredis-py can be used for pub/sub messaging between clients and Redis servers. This allows multiple clients to subscribe to specific channels and receive messages in real-time when they are published to those channels. To subscribe to a channel using aioredis-py, you can do the following:

import asyncio
import aioredis

async def main():
    redis = await aioredis.create_redis_pool('redis://localhost')
    channel, = await redis.subscribe('my_channel')
    async for message in channel.iter():
    await redis.wait_closed()

This will create a subscription to the ‘my_channel’ channel and print any messages that are published to that channel in real-time.


  1. aioredis-py can be used for caching data in Redis. Caching can greatly improve the performance of applications by reducing the amount of time it takes to retrieve frequently accessed data. For example, to cache the result of a CPU-intensive function for a period of time using aioredis-py, you can do the following:
import asyncio
import aioredis

async def get_data():
    # Some CPU-intensive operation to generate data

async def get_cached_data():
    redis = await aioredis.create_redis_pool('redis://localhost')
    data = await redis.get('cached_data')
    if data is None:
        data = await get_data()
        await redis.setex('cached_data', 60, data) # Cache data for 60 seconds
    await redis.wait_closed()
    return data

This will check if the ‘cached_data’ key exists in Redis and return it if it does. If it doesn’t, it will call the get_data() function to generate the data, cache it in Redis for 60 seconds using the setex() method, and return the data.


It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.