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, 3The 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, 3This 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 techniques to solve the problem above which are officially documented
YAML configuration example:
Lint/SharedVarInFiber:
  Enabled: trueIncluded 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. 
Class Method Summary
- 
        .parsed_doc : String | Nil
        
          Returns documentation for this rule, if there is any. 
Instance Method Summary
- #description : String
- #description=(description : String)
- #enabled=(enabled : Bool)
- #enabled? : Bool
- #excluded : Array(String) | Nil
- #excluded=(excluded : Array(String) | Nil)
- #severity : Ameba::Severity
- #severity=(severity : Ameba::Severity)
- #since_version : SemanticVersion | Nil
- #since_version=(since_version : String)
- #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(hasher)
    hash, 
    
  
    
      name
    name, 
    
  
    
      special?
    special?, 
    
  
    
      test(source : Source, node : Crystal::ASTNode, *opts)test(source : Source) test
Class methods inherited from class Ameba::Rule::Base
  
  
    
      default_severity : Ameba::Severity
    default_severity
    
  
      
  Macros inherited from class Ameba::Rule::Base
  
  
    
      issue_for(*args, **kwargs, &block)
    issue_for
    
  
    
      
      
      
      
  Macros inherited from module Ameba::Config::RuleConfig
  
  
    
      properties(&block)
    properties
    
  
    
      
      
      
      
    
      
      
      
      
    
  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, 3The 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, 3This 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 techniques to solve the problem above which are officially documented
YAML configuration example:
Lint/SharedVarInFiber:
  Enabled: trueClass Method Detail
Returns documentation for this rule, if there is any.
module Ameba
  # This is a test rule.
  # Does nothing.
  class MyRule < Ameba::Rule::Base
    def test(source)
    end
  end
end
MyRule.parsed_doc # => "This is a test rule.\nDoes nothing."