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

fix: Set Context properly #582

Closed
wants to merge 5 commits into from
Closed

fix: Set Context properly #582

wants to merge 5 commits into from

Conversation

kehiy
Copy link
Contributor

@kehiy kehiy commented Jul 8, 2023

Description

This PR introduces a global context and passed it to all modules when the node starts. By shutting down the node, the context will be cancelled and all modules will receive the cancellation signal.

Related issue(s)

@kehiy kehiy requested a review from b00f July 8, 2023 22:32
@amirvalhalla
Copy link
Member

amirvalhalla commented Jul 9, 2023

@kehiy

I thinks there are 2 issues which you should fix in your code

  1. in network/network.go when you call newNetwork you passed ctx,cancel which it's correct but after you created the network struct you used n.ctx in newDHTService(n.ctx, n.host, kadProtocolID, conf.Bootstrap, n.logger) and for 2 other sub methods just used ctx that all of them have same memory address for ctx but different style code. please use n.ctx for all of them to have a same style.

  2. in sync/sync.go you used ctx:=context.Background() which I think it would be better to create a context with cancellation, therefore when you stop sync package it will call cancel() and Done() channel of ctx will raise.

@b00f what is your opinion?

b00f
b00f previously approved these changes Jul 9, 2023
network/network.go Outdated Show resolved Hide resolved
@@ -46,7 +47,9 @@ func NewNode(genDoc *genesis.Genesis, conf *config.Config,
"version", version.Version(),
"network", genDoc.ChainType())

network, err := network.NewNetwork(conf.Network)
ctx, cancel := context.WithCancel(context.Background())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to add cancel to the node structure and call it when the node is shutting down

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I can do it, but just tell me what should I do about the network module cancel?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that but there is an issue, all stuff is fine but I got this warning:

the cancel function is not used on all paths (possible context leak)

I already called cancel in the stop node method.

Copy link
Contributor

Choose a reason for hiding this comment

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

I did that but there is an issue, all stuff is fine but I got this warning:

the cancel function is not used on all paths (possible context leak)

I already called cancel in the stop node method.

Has the Stop method been used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so.
where should I use it?

Copy link
Contributor

Choose a reason for hiding this comment

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

in GUI: CTA Exit
in CLI: on interrupt signal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok
I will add it

@kehiy
Copy link
Contributor Author

kehiy commented Jul 9, 2023

@kehiy

I thinks there are 2 issues which you should fix in your code

  1. in network/network.go when you call newNetwork you passed ctx,cancel which it's correct but after you created the network struct you used n.ctx in newDHTService(n.ctx, n.host, kadProtocolID, conf.Bootstrap, n.logger) and for 2 other sub methods just used ctx that all of them have same memory address for ctx but different style code. please use n.ctx for all of them to have a same style.
  2. in sync/sync.go you used ctx:=context.Background() which I think it would be better to create a context with cancellation, therefore when you stop sync package it will call cancel() and Done() channel of ctx will raise.

@b00f what is your opinion?

sure, I think its true
probably i will fix it

@b00f
Copy link
Collaborator

b00f commented Jul 9, 2023

@Ja7ad please review this PR

@kehiy
Copy link
Contributor Author

kehiy commented Jul 9, 2023

@b00f I added some changes!
I think that what you want, you can review it now

@kehiy kehiy requested a review from b00f July 9, 2023 22:59
@pactus-project pactus-project deleted a comment from codecov bot Jul 9, 2023
@codecov
Copy link

codecov bot commented Jul 9, 2023

Codecov Report

Merging #582 (d1c6ae9) into main (6a16d7d) will decrease coverage by 0.05%.
The diff coverage is 83.33%.

@@            Coverage Diff             @@
##             main     #582      +/-   ##
==========================================
- Coverage   83.53%   83.49%   -0.05%     
==========================================
  Files         154      154              
  Lines        7252     7257       +5     
==========================================
+ Hits         6058     6059       +1     
- Misses        915      918       +3     
- Partials      279      280       +1     

@@ -104,7 +106,8 @@ func readData(t *testing.T, r io.ReadCloser, len int) []byte {
}

func TestStoppingNetwork(t *testing.T) {
net, err := NewNetwork(testConfig())
ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

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

for test you can use context.ToDo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is that the just thing I should change?

Copy link
Contributor

@Ja7ad Ja7ad Jul 10, 2023

Choose a reason for hiding this comment

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

is that the just thing I should change?

No, in other test change Background to ToDo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I will change it too
should I remove defer func for cancel() in node.go?

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, I will change it too should I remove defer func for cancel() in node.go?

Yes

@b00f b00f requested a review from Ja7ad July 10, 2023 14:37
@b00f b00f dismissed their stale review July 10, 2023 14:37

Mistakenly approved

@b00f b00f changed the title feat: Set Context properly fix: Set Context properly Jul 10, 2023
@kehiy
Copy link
Contributor Author

kehiy commented Jul 10, 2023

@b00f should we close this and follow the steps I provided and make a new pr?
or we can resolve them here?

@b00f b00f closed this Jul 10, 2023
@b00f
Copy link
Collaborator

b00f commented Jul 10, 2023

Better to have a new PR. Thanks

@kehiy kehiy deleted the context branch July 10, 2023 18:57
@kehiy
Copy link
Contributor Author

kehiy commented Jul 10, 2023

Better to have a new PR. Thanks

okay, after the script, I will send a new PR for it.

@OmidHekayati
Copy link

Personally, I don't like using Context to solve when the requirements are just about object life cycles!

As discuss here suggest having three methods in all struct as an object to solve the object life cycle. When we have the Deinit method, anyone can easily know how an object de-initializes and reduce magic codes!

For a more deep discussion, I think it is better to have a meeting if you agree with the overall idea.

@b00f
Copy link
Collaborator

b00f commented Jul 13, 2023

For a more deep discussion, I think it is better to have a meeting if you agree with the overall idea.

We have some DEV-meeting from time to time with the development team. There's no specific time for these meetings, but we can discuss it at the next DEV-meeting.

Thanks for bringing it up @OmidHekayati

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.

Set Context properly
5 participants