{"record":{"id":"95f2717b5b20c9bf","repo":"apache/pulsar","slug":"invalid-topicname-s","errorCode":null,"errorMessage":"Invalid topicname %s","messagePattern":"Invalid topicname (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pulsar-functions/instance/src/main/python/contextimpl.py","lineNumber":217,"sourceCode":"        message_conf = {}\n      message_conf['properties'] = properties\n\n    if message_conf:\n      self.publish_producers[topic_name].send_async(\n        output_bytes, partial(self.callback_wrapper, callback, topic_name, self.get_message_id()), **message_conf)\n    else:\n      self.publish_producers[topic_name].send_async(\n        output_bytes, partial(self.callback_wrapper, callback, topic_name, self.get_message_id()))\n\n  def ack(self, msgid, topic):\n    topic_consumer = None\n    if topic in self.consumers:\n      topic_consumer = self.consumers[topic]\n    else:\n      # if this topic is a partitioned topic\n      m = re.search(r'(.+)-partition-(\\d+)', topic)\n      if not m:\n        raise ValueError('Invalid topicname %s' % topic)\n      elif m.group(1) in self.consumers:\n        topic_consumer = self.consumers[m.group(1)]\n      else:\n        raise ValueError('Invalid topicname %s' % topic)\n    topic_consumer.acknowledge(msgid)\n\n  def get_and_reset_metrics(self):\n    metrics = self.get_metrics()\n    # TODO(sanjeev):- Make this thread safe\n    self.reset_metrics()\n    return metrics\n\n  def reset_metrics(self):\n    # TODO: Make it thread safe\n    for user_metric in self.user_metrics_map.values():\n      user_metric._sum.set(0.0)\n      user_metric._count.set(0.0)\n","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/instance/src/main/python/contextimpl.py#L199-L235","documentation":"ContextImpl.ack looks up the consumer registered for the message's topic to acknowledge a message. If the topic is not a known consumer topic and does not even match the '<topic>-partition-N' naming pattern of a partitioned topic, Pulsar raises ValueError('Invalid topicname %s'), meaning ack was called with a topic this instance never subscribed to.","triggerScenarios":"Calling context.ack() with an output/ack topic name that is not in self.consumers and does not contain '-partition-N'; acknowledging a message manually whose topic string was constructed incorrectly.","commonSituations":"Passing the sink/output topic to ack instead of the input topic; typos in a hardcoded topic name; using an ack on a message obtained from a topic the instance is not consuming.","solutions":["Ack only messages received from input topics the instance is subscribed to (e.g. use the message object's own topic or context ack semantics).","Verify the topic name string exactly matches a topic in the instance's consumer map, including tenant/namespace.","If it is a partitioned topic, confirm the name ends with '-partition-<n>' and the base topic has a registered consumer.","Use context.get_input_topics() / instance config to confirm subscription names before acking."],"exampleFix":"// before\ncontext.ack('persistent://public/default/my-output-topic', msgid)\n// after\ncontext.ack('persistent://public/default/my-input-topic', msgid)  # a topic actually consumed by this instance","handlingStrategy":"validation","validationCode":"consumers = ctx.get_input_topics()  # not a direct API in all versions; verify topic membership\nif topic not in known_input_topics and not re.match(r'.+-partition-\\d+$', topic):\n    raise ValueError(f\"cannot ack unknown topic {topic}\")","typeGuard":null,"tryCatchPattern":"try:\n    context.ack(topic, msgid)\nexcept ValueError as e:\n    logging.warning('ack rejected: %s', e)  # do not ack unknown topics","preventionTips":["Only ack messages via their original input topic context.","Don't hardcode topic strings; derive from received message.","Verify input topic config matches acked names including partitions."],"tags":["python","pulsar-functions","topic-name","ack"],"backgroundTag":"invalid-topic-name","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}