class Ameba::Rule::Lint::SharedVarInFiber

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

Defined in:

ameba/rule/lint/shared_var_in_fiber.cr

Constant Summary

MSG = "Shared variable `%s` is used in fiber"

Constructors

Instance Method Summary

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

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

def self.new(config = nil) #

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

[View source]

Instance Method Detail

def description : String #

def description=(description : String) #

def enabled : Bool #

def enabled=(enabled : Bool) #

def excluded : Array(String) | Nil #

def excluded=(excluded : Array(String) | Nil) #

def severity : Ameba::Severity #

def severity=(severity : Ameba::Severity) #

def test(source, node, scope : AST::Scope) #

[View source]
def test(source) #

[View source]