Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "index out of range" when executing CallData.execute #429

Closed
qnnn opened this issue Jan 20, 2024 · 0 comments · Fixed by #430
Closed

Error "index out of range" when executing CallData.execute #429

qnnn opened this issue Jan 20, 2024 · 0 comments · Fixed by #430

Comments

@qnnn
Copy link
Contributor

qnnn commented Jan 20, 2024

Describe the bug

  1. When I execute the following script, I encounter the following error:

Error creating message from data. Data: xxx Error: invalid character '{' looking for beginning of object key string

{
  "total": 10000,
  "concurrency": 1000,
  "proto": "./cmd/ghz/grpcapi.proto",
  "proto_import_paths": [
    "./cmd/ghz/client.proto",
    "./cmd/ghz/service.proto",
    "./cmd/ghz/request.proto",
    "./cmd/ghz/response.proto",
    "./cmd/ghz/heartbeat.proto",
    "./cmd/ghz/contract.proto",
    "./cmd/ghz/model.proto"
  ],
  "call": "v1.PolarisGRPC.RegisterInstance",
  "data": {
    "id": "polaris-perf-test-1-ins-{{.RequestNumber}}",
    "service": "POLARIS-PERF-TEST-1",
    "namespace": "default",
    "host": "fakehost-{{randomString 100 }}.com",
    "port": "{{randomInt 1000 50000}}",
    "enable_health_check": true,
    "health_check": {
      "type": 1,
      "heartbeat": {
        "ttl": 3
      }
    },
    "metadata": {
      "aaaaaaaa01": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa02": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa03": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa04": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa05": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa06": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa07": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa08": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa09": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa10": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa11": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa12": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa13": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa14": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa15": "{{randomString 10 }}-aaaaa",
      "aaaaaaaa16": "{{randomString 10 }}-aaaaa"
    },
    "healthy": true,
    "isolate": false,
    "ctime": "{{.TimestampUnix}}",
    "mtime": "{{.TimestampUnix}}"
  },
  "host": "127.0.0.1:8091",
  "insecure": true
}
  1. The issue is that an error occurred during the execution of CallData.execute, but it was not handled in this code.
func (td *CallData) ExecuteData(data string) ([]byte, error) {
	if len(data) > 0 {
		input := []byte(data)
		tpl, err := td.execute(data)
                // The error is not being handled when err is not nil
		if err == nil && tpl != nil {
			input = tpl.Bytes()
		}
		return input, nil
	}

	return []byte{}, nil
}

error:

template: call_template_data:1:139: executing "call_template_data" at <randomString 100>: error calling randomString: runtime error: index out of range [1212]

  1. When I recover the stringWithCharset method, I found that the error was triggered when executing seededRand.Intn(len(charset)).
func stringWithCharset(length int, charset string) string {
    defer func() {
       if r := recover(); r != nil {
          if e, ok := r.(error); ok {
             // index out of range error
             panic(e)
          }
       }
    }()
    
    b := make([]byte, length)
    for i := range b {
       b[i] = charset[seededRand.Intn(len(charset))]
    }
    return string(b)
}
  1. root cause:

rand.Source is not safe for concurrent use by multiple goroutines. This also applies when used in rand.Rand.

image-20240121031323463

I want to fix it and add benchmark tests

Environment

  • OS: [win 10]
  • ghz: [v0.117.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant