Skip to content

xkafka: Producer.AsyncPublish causes goroutine leak when Start/Run is not called #72

@sonnes

Description

@sonnes

Summary

The Producer struct requires Start() or Run() to be called to drain the internal events channel (delivery reports). However, AsyncPublish works without Start() being called — it silently pushes messages via ProduceChannel() without any compile-time or runtime indication that the event loop is missing.

Problem

When Start() is not called:

  1. The events channel (producer.go#L17) fills up with unhandled delivery reports
  2. The confluent-kafka-go internal poller blocks because it cannot push to the full events channel
  3. ProduceChannel() backs up because the poller is blocked
  4. Any goroutine calling AsyncPublish blocks forever on p.kafka.ProduceChannel() <- km
  5. This results in an unbounded goroutine and memory leak

Reproduction

Call NewProducer() followed by AsyncPublish() repeatedly without ever calling Start() or Run(). Observe goroutine count growing without bound.

Suggested Improvements

  • Document the requirement: The current doc on Start() only says "starts kafka event handling" without explaining it is mandatory for AsyncPublish to function correctly. Add explicit documentation that Start()/Run() must be called when using AsyncPublish.
  • Return an error: Consider returning an error from AsyncPublish if Start() has not been called yet.
  • Auto-start: Alternatively, auto-start the event loop lazily on the first AsyncPublish call via sync.Once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions