shashank created CAMEL-27460:
--------------------------------

             Summary: camel-azure-iothub - a device client is closed as idle 
after a long send or a slow open, so a slow hub gets a new connection for every 
message
                 Key: CAMEL-27460
                 URL: https://issues.apache.org/jira/browse/CAMEL-27460
             Project: Camel
          Issue Type: Bug
          Components: camel-azure
            Reporter: shashank
            Assignee: shashank


h3. What happens

{{DeviceClientPool}} evicts a device client after {{deviceClientIdleTimeout}} 
without use, but the "last used" time it compares with was not kept up to date:

* {{acquire}} read the clock once, before it could block in {{getOrCreate}} for 
the whole {{open(true)}} of a new client, and stamped the lease with that old 
time.
* A new client was put in the pool with the time read before {{open(true)}}.
* When the last lease was given back, {{touchAndMaybeEvict}} compared the clock 
with that old time before updating it.

So a client was closed as "idle" right after a long lease, or just after it had 
been opened (never while it had a lease):

* a send or twin update that takes longer than {{deviceClientIdleTimeout}} 
closes its client when the lease is given back; the next message opens a new 
connection;
* when the hub is slow to accept connections and {{open(true)}} takes longer 
than {{deviceClientIdleTimeout}}, the new client is closed as soon as the first 
(fast) send gives it back, and every message pays a new slow open and a close. 
5 messages 100 ms apart opened 5 connections (idle timeout 10 s, open 15 s). 
The extra opens add load to a hub that is already slow;
* a client that was just opened can also be evicted by the eviction task or by 
a concurrent {{acquire}} before its creator leases it.

With the defaults it does not happen: the idle timeout is 5 minutes, each SDK 
call of a send is bounded by the 10 s send timeout, and the SDK gives up an 
open after about 4 minutes. It shows with a {{deviceClientIdleTimeout}} shorter 
than the time the hub takes to accept a connection or to answer a send (for 
example 10 s against a hub that needs 15 s). It stops once the hub is fast 
again.

h3. How to reproduce

A pool with idle timeout 10 s, a fake clock and a mocked {{DeviceClient}}: 
lease the client, move the clock 15 s on, give the lease back. The client is 
closed and removed, and the next lease 1 ms later opens a new client. With an 
{{open(true)}} that moves the clock 15 s on, 5 messages open 5 clients.

Found with a TLA+ model of the pool (producers, the eviction task, the SDK 
status callback and a slow hub); the model's "idle means idle" property fails 
on main.

h3. Fix

* A client with a lease is still never evicted; now its idle time counts from 
the last release: the lease touches the client when it is taken and right 
before it is given back, and giving it back no longer runs an idle check. A 
client given back after a long send is then closed by the eviction task like 
any other idle client, between the idle timeout and the idle timeout plus 
{{deviceClientEvictionInterval}} after the release.
* A new client gets its time after {{open(true)}}.
* The clock is read at each touch; the time never moves back.

The public API of the pool and the options do not change.

h3. Not changed here

* A terminal DISCONNECTED that the SDK reports before the new client is in the 
pool is lost, so the dead client is pooled and used for every later send until 
the traffic pauses for the idle timeout (low: needs the status to arrive in a 
short window).
* {{Lease.close}} can close a client that another producer leased between that 
release and a disconnect that retired it (the client is already disconnected, 
so that send fails anyway).
* {{shutdown()}} closes leased clients and does not complete the creation 
futures it clears; Camel stops the routes first, so this only matters after a 
graceful-shutdown timeout.
* A slow {{close()}} of a retired client delays the eviction task (bounded by 
one SDK reconnect attempt).

_Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to