Azure-sdk-for-js: Sample code produces TypeError: Cannot read property 'sequenceNumber' of undefined' when eventhub is empty

Created on 3 Sep 2020  路  6Comments  路  Source: Azure/azure-sdk-for-js

  • Package Name: azure/event-hubs
  • Package Version: 5.2.2
  • Operating system: Linux
  • [ ] nodejs

    • version: v13.8.0

  • Is the bug related to documentation in

Describe the bug
When running the SDK sample code for reading events (reproduced below with the timeout promise removed) from an eventhub I successfully get events BUT I also receive errors when the eventhub has nothing on it. These being 'TypeError: Cannot read property 'sequenceNumber' of undefined'.
I'm guessing that somewhere in the library some garbage is being 'taken' off the queue and interpreted as a message causing an error.

To Reproduce
Steps to reproduce the behavior:

  1. Set up a new event hub
  2. Run code and wait a while
const { EventHubConsumerClient } = require("@azure/event-hubs");
const { ContainerClient } = require("@azure/storage-blob");
const { BlobCheckpointStore } = require("@azure/eventhubs-checkpointstore-blob");

const connectionString = process.env.EHCONNECTIONSTRING;
const eventHubName = process.env.EHNAME;
const consumerGroup = "$Default"; // name of the default consumer group
const storageConnectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
const containerName = process.env.EHCONTAINER;

async function main() {
  // Create a blob container client and a blob checkpoint store using the client.
  const containerClient = new ContainerClient(storageConnectionString, containerName);
  const checkpointStore = new BlobCheckpointStore(containerClient);

  // Create a consumer client for the event hub by specifying the checkpoint store.
  const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName, checkpointStore);

  // Subscribe to the events, and specify handlers for processing the events and errors.
  const subscription = consumerClient.subscribe({
      processEvents: async (events, context) => {
        for (const event of events) {
          console.log(`Received event: '${event.body}' from partition: '${context.partitionId}' and consumer group: '${context.consumerGroup}'`);
        }
        // Update the checkpoint.
        await context.updateCheckpoint(events[events.length - 1]);
      },

      processError: async (err, context) => {
        console.log(`Error : ${err}`);
      }
    }
  );
}

main().catch((err) => {
  console.log("Error occurred: ", err);
});

Expected behavior
No errors produced

Screenshots
If applicable, add screenshots to help explain your problem.
image

Client Docs Event Hubs blocking-release bug customer-reported

All 6 comments

Thanks for reporting @robeving

A stack trace with the error would help a lot.
Can you update the console log statement in processError from

console.log(`Error : ${err}`);

to

console.log(`Error : `, err);

This would print the entire error instead of just the error message.

@ramya-rao-a , @robeving - looks like this is a simple one. EventHubs will return zero events if the wait time expires with no messages arriving (as you're seeing).

This is useful if you just want an indicator that receiving is still active but it means that this line in our sample is bad when events is empty.

  await context.updateCheckpoint(events[events.length - 1]);

I'll update the samples. The simplest fix is just to check that events.length === 0 so your processEvents method is similar to this:

   processEvents: async (events, context) => {
       if (events.length === 0) {
          console.log(`No events received within wait time. Waiting for next interval`);
          return;
        }

        for (const event of events) {
          console.log(`Received event: '${event.body}' from partition: '${context.partitionId}' and consumer group: '${context.consumerGroup}'`);
        }
        // Update the checkpoint.
        await context.updateCheckpoint(events[events.length - 1]);
      },

We have this concept in a few spots so I'll need to validate them all:

  • [x] EventHubs checkpoint store samples
  • [x] EventHubs checkpoint store readme
  • [x] EventHubs readme

Thanks @richardpark-msft

Let's fix this for the upcoming release. Added the right labels and milestone

The docs on the Microsoft website still show up with the outdated version of this code:
https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-node-get-started-send

MS Azure Event Hub JS docs still shows the outdated code. Good thing I found this issue.

Thank you @nVitius and @seanquijote, working on getting that updated.

Was this page helpful?
0 / 5 - 0 ratings