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 a bug that doesn't release lock #79

Merged
merged 2 commits into from
Feb 26, 2020
Merged

Fix a bug that doesn't release lock #79

merged 2 commits into from
Feb 26, 2020

Conversation

lam2003
Copy link
Contributor

@lam2003 lam2003 commented Feb 25, 2020

err!=nil时未释放锁,使用defer p.lock.Unlock()修复


name: Pull request
about: Propose changes to the code
title: ''
labels: ''
assignees: ''

1. Are you opening this pull request for bug-fixs, optimizations or new feature?

2. Please describe how these code changes achieve your intention.

3. Please link to the relevant issues (if any).

4. Which documentation changes (if any) need to be made/updated because of this PR?

4. Checklist

  • I have squashed all insignificant commits.
  • I have commented my code for explaining package types, values, functions, and non-obvious lines.
  • I have written unit tests and verified that all tests passes (if needed).
  • I have documented feature info on the README (only when this PR is adding a new feature).
  • (optional) I am willing to help maintain this change if there are issues with it later.

err!=nil时未释放锁,使用defer p.lock.Unlock()修复
@codecov
Copy link

codecov bot commented Feb 25, 2020

Codecov Report

Merging #79 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #79      +/-   ##
==========================================
+ Coverage   95.61%   95.66%   +0.05%     
==========================================
  Files           8        8              
  Lines         524      531       +7     
==========================================
+ Hits          501      508       +7     
  Misses         16       16              
  Partials        7        7
Impacted Files Coverage Δ
pool.go 95.03% <100%> (+0.1%) ⬆️
pool_func.go 93.9% <0%> (+0.07%) ⬆️
ants.go 93.18% <0%> (+0.32%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 809379e...e7a2d37. Read the comment docs.

@lam2003 lam2003 requested a review from panjf2000 February 25, 2020 08:42
pool.go Outdated
@@ -247,6 +247,7 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
}
worker.recycleTime = time.Now()
p.lock.Lock()
defer p.lock.Unlock()
Copy link
Owner

Choose a reason for hiding this comment

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

在这种性能极度敏感的库代码里尽量不要用 defer,它在汇编层面会有很多连锁调用导致性能损耗。直接在下面的 if err != nil {} 里面加一个解锁的步骤就行了。

Copy link
Contributor

Choose a reason for hiding this comment

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

defer 在 1.14 基本已经没啥损耗了。另外即使是在老版本,大概率池子里运行的函数本身远重于 defer。这个池子本身的性能瓶颈也不会在这里。反倒是因为不用 defer,这已经是第二次碰到异常路径上没 unlock 的问题了。

Copy link
Owner

Choose a reason for hiding this comment

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

能看出具体是哪一步没 unlock 吗?

@panjf2000 panjf2000 changed the title Update pool.go Fix a bug that doesn't release lock when error occurs in putting worker back to pool Feb 26, 2020
@panjf2000 panjf2000 changed the title Fix a bug that doesn't release lock when error occurs in putting worker back to pool Fix a bug that doesn't release lock Feb 26, 2020
@lam2003 lam2003 requested a review from panjf2000 February 26, 2020 03:10
@panjf2000 panjf2000 added the pending merged This PR has been reviewed and approved label Feb 26, 2020
@panjf2000 panjf2000 merged commit d8cb036 into panjf2000:master Feb 26, 2020
@choleraehyq
Copy link
Contributor

@panjf2000 这种 case 不加单测么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending merged This PR has been reviewed and approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants