influxdb-client-go icon indicating copy to clipboard operation
influxdb-client-go copied to clipboard

Invalid server URL format (IP without scheme) results in panic while calling WriteAPIBlocking

Open judehung opened this issue 5 months ago • 0 comments

Specifications

  • Client Version: 2.14.0
  • InfluxDB Version: None
  • Platform: Ubuntu 24.02 arm64

Steps to reproduce

I intentionally provided an invalid server URL (an IP address without scheme) when creating a client. This causes a panic when calling WriteAPIBlocking to initialize the Write API. The sample code below reproduces the issue:

package main

import (
	"context"
	"fmt"
	"time"

	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)

func main() {
	// InfluxDB connection settings
	const (
		token  = "custom-token"
		url    = "192.168.1.104:8086"
		org    = "judehung"
		bucket = "jude-bucket"
	)

	// Create a new InfluxDB client
	client := influxdb2.NewClient(url, token)
	defer client.Close()

	// Get the write API
	writeAPI := client.WriteAPIBlocking(org, bucket)

	// Create a point to write
	point := influxdb2.NewPoint(
		"temperature",                           // Measurement name
		map[string]string{"location": "office"}, // Tags
		map[string]interface{}{"value": 23.5},   // Fields
		time.Now(),                              // Timestamp
	)

	// Write the point to InfluxDB
	err := writeAPI.WritePoint(context.Background(), point)
	if err != nil {
		fmt.Printf("Error writing to InfluxDB: %v\n", err)
		return
	}

	fmt.Println("Data written successfully to InfluxDB!")
}

Expected behavior

I understand that the current NewClient(url, token) API doesn't return an error during client creation, even with an improperly formatted URL. However, it might be a better design to validate the URL and return an error upfront—such as when an IP address is provided without a scheme—to help prevent runtime panics for library users.

func NewClient(serverURL string, authToken string) (Client, error)

Actual behavior

A panic will directly occur when invoking on client.WriteAPIBlocking(org, bucket)

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x102567698]

goroutine 1 [running]:
net/url.(*URL).ResolveReference(0x0, 0x14000158360)
	/usr/local/go/src/net/url/url.go:1115 +0x88
net/url.(*URL).Parse(0x0, {0x1026a3104?, 0xb?})
	/usr/local/go/src/net/url/url.go:1103 +0x4c
github.com/influxdata/influxdb-client-go/v2/internal/write.NewService({0x1026a39e6, 0x8}, {0x1026a490d, 0xb}, {0x1027c8490, 0x140001600a0}, 0x14000167260)
	/Users/judehung/go/pkg/mod/github.com/influxdata/influxdb-client-go/[email protected]/internal/write/service.go:77 +0x7c
github.com/influxdata/influxdb-client-go/v2/api.NewWriteAPIBlocking(...)
	/Users/judehung/go/pkg/mod/github.com/influxdata/influxdb-client-go/[email protected]/api/writeAPIBlocking.go:63
github.com/influxdata/influxdb-client-go/v2.(*clientImpl).WriteAPIBlocking(0x14000142240, {0x1026a39e6, 0x8}, {0x1026a490d, 0xb})
	/Users/judehung/go/pkg/mod/github.com/influxdata/influxdb-client-go/[email protected]/client.go:239 +0x150
main.main()
	/Users/judehung/MyWork/Repos/PersonalTry/influx-test/main.go:25 +0x94

Additional info

No response

judehung avatar Jun 02 '25 07:06 judehung