Skip to content

Conversation

4566704
Copy link

@4566704 4566704 commented May 20, 2025

NewJetProducer 创建 JetProducer struct时没有赋值 ctx,因为JetProducer的一些方法要用到ctx,ctx为nil引发panic:

Greptile Summary

Fixed a critical nil context issue in the NATS JetStream producer implementation that was causing panics during stream operations.

  • Added missing context parameter to natsq/producer.go:NewJetProducer() constructor and properly initialized it in the JetProducer struct
  • Updated example/natsq/publisher/publisher.go to pass context.Background() to NewJetProducer calls
  • Error handling in example/natsq/publisher/publisher.go should be improved for CreateOrUpdateStream operations
  • Resource cleanup could be improved by using defer for Close() calls in publisher example

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

@@ -29,7 +30,7 @@ func main() {
p.Close()

// JetMode
j, _ := natsq.NewJetProducer(&c)
j, _ := natsq.NewJetProducer(&c, context.Background())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Error handling is ignored. Could lead to nil pointer dereference if producer creation fails.

Suggested change
j, _ := natsq.NewJetProducer(&c, context.Background())
j, err := natsq.NewJetProducer(&c, context.Background())
if err != nil {
log.Fatalf("Error creating jet producer: %v", err)
}

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@4566704
Copy link
Author

4566704 commented May 28, 2025

BUG说明:
1.修复前 example/natsq/publisher/publisher.go 是没办法正常运行的,会引发panic
2.JetProducer struct 定义了 ctx context.Context,但没有赋值,JetProducer的一些方法使用了它。

修复说明:
1.NewJetProducer方法追加了ctx 参数。
2.尽可能少改动代码和不新增方法。

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 this pull request may close these issues.

1 participant