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 techniques 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

Class Method Summary

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

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

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 techniques to solve the problem above which are officially documented

YAML configuration example:

Lint/SharedVarInFiber:
  Enabled: true

[View source]

Class Method Detail

def self.parsed_doc : String | Nil #

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."

Instance Method Detail

def description : String #

def description=(description : String) #

def enabled=(enabled : Bool) #

def 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]