Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.
This repository was archived by the owner on Mar 17, 2026. It is now read-only.

Topic.flush() triggering INVALID_ARGUMENT error when queue is empty #1212

@ettancos

Description

@ettancos

Hi,
We are upgrading our lib and happily noticed the explicit support for flushing the queue before exit. Unfortunately flush seems to cause an error when the queue is empty. To reproduce one only needs to call flush when the queue is empty, e.g: use batch size 1 or await on the returned promise. Occasionnally this could happen with batching enabled too. Below is a simple publisher in typescript to reproduce. We have traced this to here, shouldn't it check if the queue is empty before calling _publish() or inside _publish before making the request?

Environment details

  • OS: Arch linux 5.10.16-1
  • Node.js version: 12.19.1
  • npm version: 6.14.8
  • @google-cloud/pubsub version: 2.9.0

Steps to reproduce

import { PubSub } from '@google-cloud/pubsub'
import { v4 } from 'uuid'

import { PublishOptions } from '@google-cloud/pubsub/build/src/publisher'

const publishOptions: PublishOptions = { batching: { maxMessages: 1} }

process.on('unhandledRejection', reason => console.log('unhandled', reason))
const pubSub = new PubSub({ projectId: 'ems-mobile-engage-staging' })

async function run () {
  const topic = pubSub.topic('test', publishOptions)
  await topic.get()
  let count = 0

  while (count < 2) {
    const payload = { title: 'test', idx: count, uuid: v4() }
    
    topic.publishMessage({ json: payload })

    console.log('published', payload)
    count += 1
  }

  await topic.flush()
}

run()
  .then(() => pubSub.close().catch(err => console.log('close', err)))
  .then(() => console.log('done'))
  .catch(err => console.log('err', err))

This will result in the following:

published { title: 'test', idx: 0, uuid: '7d5af9d6-1cee-4404-8863-6978c2cadc95' }
published { title: 'test', idx: 1, uuid: '02e6bf26-7e79-4231-82cc-41992faa34a5' }
err Error: 3 INVALID_ARGUMENT: The value for message_count is too small. You passed 0 in the request, but the minimum value is 1.
    at Object.callErrorFromStatus (/home/test/pubsub-test/node_modules/@grpc/grpc-js/src/call.ts:81:24)
    at Object.onReceiveStatus (/home/test/pubsub-test/node_modules/@grpc/grpc-js/src/client.ts:334:36)
    at Object.onReceiveStatus (/home/test/pubsub-test/node_modules/@grpc/grpc-js/src/client-interceptors.ts:426:34)
    at Object.onReceiveStatus (/home/test/pubsub-test/node_modules/@grpc/grpc-js/src/client-interceptors.ts:389:48)
    at /home/test/pubsub-test/node_modules/@grpc/grpc-js/src/call-stream.ts:249:24
    at processTicksAndRejections (internal/process/task_queues.js:79:11) {
  code: 3,
  details: 'The value for message_count is too small. You passed 0 in the request, but the minimum value is 1.',
  metadata: Metadata { internalRepr: Map {}, options: {} },
  note: 'Exception occurred in retry method that was not classified as transient'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: pubsubIssues related to the googleapis/nodejs-pubsub API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions