question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The producer attempted a transactional operation in an invalid state

See original GitHub issue

So, using the example given here, I tried to set up a transactional producer.

client:

const client = new Kafka({
  clientId: 'my-client',
  brokers: ['my-broker'],
})
const producer = client.producer({ 
  maxInFlightRequests: 1, 
  idempotent: true,
  transactionalId: 'my-transaction'
})

transaction:

const  transaction = await producer.transaction()

try {
  await transaction.send({ topic, messages })

  await transaction.commit()
} catch (e) {
  await transaction.abort()
}

As long as I send and commit without issue, it works. However, if any error is thrown, and abort is called, I get the following error:

[Connection] Response EndTxn(key: 26, version: 0)
    broker: "my-broker"
    clientId: "my-client"
    error: "The producer attempted a transactional operation in an invalid state"
    correlationId: 1
    size: 10
    {
      "type": "KafkaJSProtocolError",
      "message": "The producer attempted a transactional operation in an invalid state",
      "stack":
          KafkaJSProtocolError: The producer attempted a transactional operation in an invalid state
              at createErrorFromCode (/Users/eliw00d/Documents/GitHub/my-project/node_modules/kafkajs/src/protocol/error.js:536:10)
              at Object.parse (/Users/eliw00d/Documents/GitHub/my-project/node_modules/kafkajs/src/protocol/requests/endTxn/v0/response.js:24:11)
              at Connection.send (/Users/eliw00d/Documents/GitHub/my-project/node_modules/kafkajs/src/network/connection.js:306:35)
              at process._tickCallback (internal/process/next_tick.js:68:7)
      "name": "KafkaJSProtocolError",
      "retriable": false,
      "code": 48
    }

To better illustrate the issue, I even tried:

const transaction = await producer.transaction()
await transaction.abort()

with the same result.

Is this a bug or am I missing something?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
konapuncommented, Sep 17, 2019

(Disclaimer: I’m not a kafkajs maintainer) I’ve looked into this a bit and it looks like the error is caused by the kafka backend not transitioning to a transacting state until producer.send is called so if the kafkajs client attempts to abort a transaction before the send is attempted you’ll receive that error.

To get around this I’m wrapping the abort in a try/catch and swallowing the error if its code is 48:

try {
    transaction.abort()
} catch (err) {
    if (err.code !== 48) {
        throw err
    }
}

This works for now but I would like to see the actual bug get fixed in kafkajs.

1reaction
tulioscommented, Oct 1, 2019

@eliw00d I like your proposal of a transaction.isAbortable(), I will check how the Java client work with this use-case and I can start a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kafka producer throws an error Invalid transition attempted ...
If I call producer.initTransactions(); again before starting transaction 2, it throws an exception "Invalid transition attempted from state ...
Read more >
Kafka producer failed with InvalidTxnStateException when ... - Re
Hi Becket, I have reproduced this problem in our development environment. Below is the log message with debug level. Seems that the exception...
Read more >
Re: Kafka producer failed with InvalidTxnStateException when ...
Hi Becket, One more thing, I have tried to restart other brokers without active controller, but this exception might happen as well.
Read more >
the producer attempted to use a producer id which is not ...
I'm always receiving an exception saying that "The producer attempted to use a producer id which is not currently assigned to its transactional...
Read more >
kafka - Go Documentation Server
Retriable errors: Some error cases allow the attempted operation to be retried, ... Broker: Producer attempted a transactional operation in an invalid state ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found