class Ameba::Rule::Lint::SharedVarInFiber
- Ameba::Rule::Lint::SharedVarInFiber
- Ameba::Rule::Base
- Reference
- Object
Overview
A rule that disallows using shared variables in fibers, which are mutated during iterations.
In most cases it leads to unexpected behaviour and is undesired.
For example, having this example:
n = 0
channel = Channel(Int32).new
while n < 3
n = n + 1
spawn { channel.send n }
end
3.times { puts channel.receive } # => # 3, 3, 3
The problem is there is only one shared between fibers variable n
and when channel.receive
is executed its value is 3
.
To solve this, the code above needs to be rewritten to the following:
n = 0
channel = Channel(Int32).new
while n < 3
n = n + 1
m = n
spawn do { channel.send m }
end
3.times { puts channel.receive } # => # 1, 2, 3
This rule is able to find the shared variables between fibers, which are mutated during iterations. So it reports the issue on the first sample and passes on the second one.
There are also other technics to solve the problem above which are officially documented
YAML configuration example:
Lint/SharedVarInFiber:
Enabled: true
Included Modules
- YAML::Serializable
- YAML::Serializable::Strict
Defined in:
ameba/rule/lint/shared_var_in_fiber.crConstant Summary
-
MSG =
"Shared variable `%s` is used in fiber"
Constructors
- .new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
-
.new(config = nil)
A rule that disallows using shared variables in fibers, which are mutated during iterations.
Instance Method Summary
- #description : String
- #description=(description : String)
- #enabled : Bool
- #enabled=(enabled : Bool)
- #excluded : Array(String) | Nil
- #excluded=(excluded : Array(String) | Nil)
- #severity : Ameba::Severity
- #severity=(severity : Ameba::Severity)
- #test(source, node, scope : AST::Scope)
- #test(source)
Instance methods inherited from class Ameba::Rule::Base
==(other)
==,
catch(source : Source)
catch,
excluded?(source)
excluded?,
group
group,
hash
hash,
name
name,
special?
special?,
test(source : Source, node : Crystal::ASTNode, *opts)test(source : Source) test
Class methods inherited from class Ameba::Rule::Base
parsed_doc
parsed_doc
Constructor Detail
A rule that disallows using shared variables in fibers, which are mutated during iterations.
In most cases it leads to unexpected behaviour and is undesired.
For example, having this example:
n = 0
channel = Channel(Int32).new
while n < 3
n = n + 1
spawn { channel.send n }
end
3.times { puts channel.receive } # => # 3, 3, 3
The problem is there is only one shared between fibers variable n
and when channel.receive
is executed its value is 3
.
To solve this, the code above needs to be rewritten to the following:
n = 0
channel = Channel(Int32).new
while n < 3
n = n + 1
m = n
spawn do { channel.send m }
end
3.times { puts channel.receive } # => # 1, 2, 3
This rule is able to find the shared variables between fibers, which are mutated during iterations. So it reports the issue on the first sample and passes on the second one.
There are also other technics to solve the problem above which are officially documented
YAML configuration example:
Lint/SharedVarInFiber:
Enabled: true