Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure service bus sender connection lost after some time. #38970

Open
orizvida opened this issue Dec 21, 2024 · 1 comment
Open

Azure service bus sender connection lost after some time. #38970

orizvida opened this issue Dec 21, 2024 · 1 comment
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Bus

Comments

@orizvida
Copy link

orizvida commented Dec 21, 2024

  • Package Name: azure-servicebus
  • Package Version: 7.13.0
  • Operating System: Mac OS
  • Python Version: 3.10

Describe the bug
I create an service bus client and senders for each queue on system start up.
after about 10 minutes of inactivity (No message sent from the client) the senders lose connection and the next send takes about 8 seconds.

To Reproduce
Steps to reproduce the behavior:

  1. Send first message
  2. Wait 10 seconds
  3. Send second message

Expected behavior
A connection is always established and a send operation does not take long time.

Attaching code:
`
import asyncio
import logging
import time
import uuid
from datetime import datetime

from azure.servicebus.aio import ServiceBusClient, ServiceBusSender
from azure.servicebus import ServiceBusMessage

from src.config import settings
from src.vendor.service_bus.consts import (
EMAIL_QUEUE,
SMS_QUEUE,
WEBHOOK_QUEUE,
)

log = logging.getLogger(name)

class ServiceBusService:

def __init__(self):
    self.namespace_connection_str = settings.SERVICE_BUS_CONNECTION_STRING
    self.client = ServiceBusClient.from_connection_string(
        conn_str=self.namespace_connection_str,
    )

    t0 = time.perf_counter()
    self.senders = self.get_senders()
    x = self.senders[WEBHOOK_QUEUE]
    print(f"create senrder in ms {time.perf_counter() - t0}")
    self.locks = self.get_senders_locks()

def get_sender(self, queue_name: str) -> ServiceBusSender:
    return self.senders[queue_name]

# https://github.com/Azure/azure-sdk-for-python/issues/35618
def get_senders_locks(self) -> dict[str, asyncio.Lock]:
    locks = {}
    for queue_name in self.senders.keys():
        locks[queue_name] = asyncio.Lock()

    return locks

def get_senders(self):
    try:
        return {
            EMAIL_QUEUE: self.client.get_queue_sender(queue_name="email"),
            SMS_QUEUE: self.client.get_queue_sender(queue_name="sms"),
            WEBHOOK_QUEUE: self.client.get_queue_sender(queue_name="webhook"),
        }
    except Exception as e:
        log.error(f"Failed to initialize service bus senders: {repr(e)}")
        raise e

async def send_single_message(
    self,
    message: str,
    queue_name: str,
    scheduled_time: datetime | None = None
):
    t0 = time.perf_counter()
    sb_message = ServiceBusMessage(body=message, content_type="application/json")
    print("create sb_message: ", time.perf_counter() - t0)
    t0 = time.perf_counter()
    sender = self.get_sender(queue_name)
    print("get sender: ", time.perf_counter() - t0)
    t0 = time.perf_counter()
    sender_lock = self.locks[queue_name]
    print("get sender_lock: ", time.perf_counter() - t0)

    t0 = time.perf_counter()
    async with sender_lock:
        if scheduled_time:
            await sender.schedule_messages(messages=[sb_message], schedule_time_utc=scheduled_time)
            return
        x0 = time.perf_counter()
        await sender.send_messages(sb_message)
        print("send message inner: ", time.perf_counter() - x0)
    print("send message: ", time.perf_counter() - t0)

async def close(self):
    await self.client.close()
    for sender in self.senders.values():
        await sender.close()

service_bus_service = ServiceBusService()

async def run_test():
t0 = time.perf_counter()
await service_bus_service.send_single_message("Test", WEBHOOK_QUEUE)
t1 = time.perf_counter()
print("time in seconds: ", t1 - t0)

await asyncio.sleep(601)

t0 = time.perf_counter()
await service_bus_service.send_single_message("Test", WEBHOOK_QUEUE)
t1 = time.perf_counter()
print("2nd time in seconds: ", t1 - t0)

if name == 'main':
asyncio.run(run_test())
`

first message took 1 second
second message 8 seconds.

The entire 8 seconds were taken from the "await sender.send_messages(sb_message)" call.

@github-actions github-actions bot added Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Bus labels Dec 21, 2024
Copy link

Thank you for your feedback. Tagging and routing to the team member best able to assist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Bus
Projects
None yet
Development

No branches or pull requests

2 participants