azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Partition Pumps stuck - Exceptions never caught

Open ian-young-sydney opened this issue 7 years ago • 8 comments

During around 24 hours of running of this EPH code, I am finding that partition-pumps eventually get stuck on a single partition and never finish. The lease by that process is maintained and won't automatically be taken over by any other processes. This results in all data in that partition to pause for ever until the entire process is restarted or a new process decides to take over that partition.

In the logs I am noticing the message "Task exception was never retrieved" which is a general error from the aysncio library when an exception is thrown but never caught by the user code.

In order to see which part of the code was not picking up the error, I put in some changes in various parts to pick up the problem, and this code seems to miss the exception being thrown. See the partition_pump.py code with changes below:

    async def open_async(self):
        """
        Opens partition pump.
        """
        self.set_pump_status("Opening")
        self.partition_context = PartitionContext(self.host, self.lease.partition_id,
                                                  self.host.eh_config.client_address,
                                                  self.host.eh_config.consumer_group,
                                                  self.loop)
        self.partition_context.lease = self.lease
        self.partition_context.event_processor_context = self.lease.event_processor_context
        self.processor = self.host.event_processor(self.host.event_processor_params)
        try:
            await self.processor.open_async(self.partition_context)
        except Exception as err:  # pylint: disable=broad-except
            # If the processor won't create or open, only thing we can do here is pass the buck.
            # Null it out so we don't try to operate on it further.
            await self.process_error_async(err)
            self.processor = None
            self.set_pump_status("OpenFailed")

        # If Open Async Didn't Fail call OnOpenAsync
        try:
            if self.pump_status == "Opening":
                await self.on_open_async()
        except Exception as err:
            _logger.error(
                "%r,%r PartitionPumpError: Exception never caught here and never set to 'OpenFailed': %r",
                self.host.guid, self.partition_context.partition_id, err)
            raise err

After this code was added I got the following logs after about 10 hours of running:

uamqp.authentication.cbs_auth_async INFO     Authentication Put-Token failed. Retrying.
uamqp.c_uamqp INFO     b'put token called while closed or in error' (b'/data/src/vendor/azure-uamqp-c/src/cbs.c':b'cbs_put_token_async':483)
uamqp.async_ops.client_async INFO     CBS session pending b'EHReceiver-...-partition11'.
uamqp.async_ops.client_async INFO     Closing exclusive connection b'EHReceiver-...-partition11'.
uamqp.async_ops.connection_async INFO     Shutting down connection b'EHReceiver-....-partition11'.
uamqp.authentication.cbs_auth_async INFO     Shutting down CBS session on connection: b'EHReceiver-...-partition11'.
uamqp.authentication.cbs_auth_async INFO     Auth closed, destroying session on connection: b'EHReceiver-...-partition11'.
uamqp.c_uamqp INFO     b'Connection not open' (b'/data/src/vendor/azure-uamqp-c/src/connection.c':b'connection_encode_frame':2048)
uamqp.authentication.cbs_auth_async INFO     Finished shutting down CBS session on connection: b'EHReceiver-...-partition11'.
uamqp.c_uamqp INFO     b'send called while not open' (b'/data/src/vendor/azure-uamqp-c/src/saslclientio.c':b'saslclientio_send_async':1181)
uamqp.c_uamqp INFO     b'Cannot send encoded bytes' (b'/data/src/vendor/azure-uamqp-c/src/connection.c':b'on_bytes_encoded':268)
uamqp.c_uamqp INFO     b'saslclientio_close called while not open' (b'/data/src/vendor/azure-uamqp-c/src/saslclientio.c':b'saslclientio_close_async':1130)
uamqp.c_uamqp INFO     b'xio_close failed' (b'/data/src/vendor/azure-uamqp-c/src/connection.c':b'on_bytes_encoded':272)
uamqp.connection INFO     Connection b'EHReceiver-...-partition11' state changed from <ConnectionState.ERROR: 14> to <ConnectionState.END: 13>
uamqp.connection INFO     Connection b'EHReceiver-...-partition11' state changed from <ConnectionState.END: 13> to <ConnectionState.END: 13>
uamqp.c_uamqp INFO     b'saslclientio_close called while not open' (b'/data/src/vendor/azure-uamqp-c/src/saslclientio.c':b'saslclientio_close_async':1130)
uamqp.c_uamqp INFO     b'xio_close failed' (b'/data/src/vendor/azure-uamqp-c/src/connection.c':b'connection_close':1437)
uamqp.async_ops.connection_async INFO     Connection shutdown complete b'EHReceiver-....-partition11'.
'eventhub.pysdk-ef528926': All clients failed to start.
'...','11' PartitionPumpError: Exception never caught here and never set to 'OpenFailed':
Task exception was never retrieved

I haven't tried dealing with this exception and setting the pump to failed yet as I don't know what the flow on effect of that may be to other parts of the code. There may be more changes than just this to keep the partition pumps stable.

Note, this has some similarities to https://github.com/Azure/azure-event-hubs-python/issues/79 but in that instance the synchronous code wasn't running in a separate thread. I have some sync code but running in a thread via "loop.run_in_executor(ThreadPoolExecutor(), ....)".

ian-young-sydney avatar Aug 23 '19 00:08 ian-young-sydney

Hi @ian-young-sydney thanks for letting us know. We will take a look at this

kaerm avatar Aug 26 '19 23:08 kaerm

@ian-young-sydney

Thanks for reporting the issue.

I'm unable to reproduce the issue for now. Could you tell me which python version/os platform you are using? Besides could you share me with the EventProcessor code you wrote?

However, I confirm that current code on error handling related to opening eventhub clients isn't robust enough. E.g. Exceptions aren't raised as expected, failed open operations don't get retried.

I had a feeling that this maybe related to the issue you opened here. I've updated the code in eh_partition_pump.py to make it more robust.

I've made a patch and build a wheel for you: azure-eventhub.zip You could unzip azure-eventhub.zip and then install the wheel file by command pip install azure_eventhub-1.4.0a1-py2.py3-none-any.whl to check if this patch help you solve the problem.

Let me know what else I can do to help you.

yunhaoling avatar Sep 04 '19 00:09 yunhaoling

Thanks for the patch. When I first ran it I got some errors partly because I think it's calling the wrong process_error_async function.

        if not _opened_ok:
            await self.processor.process_error_async(self.partition_context, last_exception)
            self.set_pump_status("OpenFailed")

I think it should be calling self.process_error_async() instead. I added this fix and it didn't fall over in that spot anymore. I still need to run the new code in more realistic scenarios for a long time to see if it's not getting stuck on the partitions. I'll let you know how the run overnight goes.

ian-young-sydney avatar Sep 11 '19 05:09 ian-young-sydney

@ian-young-sydney

The design here is that eachEventHubPartitionPump object is responsible for one partition, and instantiates its own EventProcessor (self.processor). That's why it's calling self.processor.process_error_async.

Thanks, please do let me know the result to see whether I can provide further help for you.

yunhaoling avatar Sep 12 '19 22:09 yunhaoling

@yunhaoling I hear you regarding the EventHubPartitionPump instantiating the EventProcessor, but it's just that the Parent class ('PartitionPump') calls the processors process_error_async function anyway. I won't harp on this as the result is the same, so overall the patch looks good.

As for stability of the patched code, I ran tests a 5 day test for over 10 individual consumers, each consuming from 16 partitions. it looks very stable now as I couldn't detect any partitions getting stuck like before.

Thanks!

ian-young-sydney avatar Sep 18 '19 05:09 ian-young-sydney

Thanks for working with Microsoft on GitHub! Tell us how you feel about your experience using the reactions on this comment.

ghost avatar Sep 18 '19 05:09 ghost

@ian-young-sydney

Yup, what you said is right - the process_error_async on PartitionPump is just calling the processors process_error_async method. The result is the same.

I'm glad the patch works for you! Thanks for sharing the result. I'll release a new version of the package including this patch to pypi and let you know when it is published.

yunhaoling avatar Sep 18 '19 18:09 yunhaoling

Hello @ian-young-sydney

Latest update for you: azure-eventhub 1.3.2 including this patch has been released to pypi: https://pypi.org/project/azure-eventhub/1.3.2/. You can pip install azure-eventhub to get the latest version.

Thanks!

yunhaoling avatar Sep 19 '19 17:09 yunhaoling