{"record":{"id":"4595fd926b805d1e","repo":"t8y2/dbx","slug":"cannot-delete-the-virtual-host-s-while-connecte","errorCode":null,"errorMessage":"Cannot delete the virtual host '%s' while connected to it","messagePattern":"Cannot delete the virtual host '(.+?)' while connected to it","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/rabbitmq/operations.go","lineNumber":374,"sourceCode":"}\n\nfunc namespaceName(params jsonObject) (string, error) {\n\tname := stringOrEmpty(params, \"namespace\")\n\tif strings.TrimSpace(name) == \"\" {\n\t\treturn \"\", errors.New(\"namespace is required\")\n\t}\n\tif strings.TrimSpace(name) == \"*\" {\n\t\treturn \"\", errors.New(\"namespace create/delete requires a specific virtual host (all-vhosts context)\")\n\t}\n\treturn name, nil\n}\n\nfunc assertNamespaceDeletable(namespace, connectedVhost string) error {\n\tif namespace == \"/\" {\n\t\treturn errors.New(\"The default virtual host '/' cannot be deleted\")\n\t}\n\tif connectedVhost != \"\" && namespace == connectedVhost {\n\t\treturn fmt.Errorf(\"Cannot delete the virtual host '%s' while connected to it\", namespace)\n\t}\n\treturn nil\n}\n\nfunc (s *server) listExchanges(params jsonObject) (any, error) {\n\tconnection, err := s.requireConnectionConfig(params)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tallVhosts := allVhostsRequested(params)\n\texchanges, err := managementGetAll(connection, managementListPath(params, connection, \"exchanges\"))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tresult := make([]jsonObject, 0, len(exchanges))\n\tfor _, value := range exchanges {\n\t\texchange, ok := value.(map[string]any)\n\t\tif !ok {","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/rabbitmq/operations.go#L356-L392","documentation":"Deleting a virtual host while the driver's AMQP connection is still attached to it leaves the connection invalid and can break cleanup. The driver proactively refuses to delete the vhost its own connection is using.","triggerScenarios":"Calling deleteNamespace with a namespace equal to the vhost the current connection config points at (connectedVhost), e.g. deleting 'myvhost' while connected to 'myvhost'.","commonSituations":"Test teardown scripts that delete the namespace they are still using, re-running setup scripts after a connection was re-created against the same vhost, copy-pasting a delete call before switching connections.","solutions":["Connect (or reconfigure the connection) to a different vhost, e.g. the default '/', before deleting the target vhost","Delete a different namespace name if the call used the wrong one","Check the current connection's vhost and compare it to the delete target in your orchestration code"],"exampleFix":"// before\nawait client.deleteNamespace({\"namespace\": \"myvhost\"}) // while connected to myvhost\n// after\nawait client.switchConnection({\"vhost\": \"admin\"})\nawait client.deleteNamespace({\"namespace\": \"myvhost\"})","handlingStrategy":"try-catch","validationCode":"if (ns === currentConnection.vhost && ns !== \"/\") {\n  throw new Error(`switch connection off vhost '${ns}' before deleting it`)\n}","typeGuard":null,"tryCatchPattern":"try {\n  await client.deleteNamespace({ namespace })\n} catch (e) {\n  if (String(e.message).includes(\"while connected to it\")) {\n    await client.switchConnection({ vhost: \"admin\" })\n    await client.deleteNamespace({ namespace })\n  } else { throw e }\n}","preventionTips":["Track the active vhost of your connection and compare before deletes","Use a dedicated admin connection (different vhost) for namespace lifecycle operations","Filter the current vhost out of teardown lists"],"tags":["rabbitmq","vhost","lifecycle","guard"],"backgroundTag":"resource-in-use","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}