Skip to content

Instantly share code, notes, and snippets.

@tsub
Last active March 15, 2020 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsub/72e60233ed82a8a453428ea7441e6017 to your computer and use it in GitHub Desktop.
Save tsub/72e60233ed82a8a453428ea7441e6017 to your computer and use it in GitHub Desktop.
Test codes to reproduce not thread-safe errors of Dynamoid
require 'dynamoid'
Aws.config.update({
region: 'us-west-2',
credentials: Aws::Credentials.new('test', 'test'),
})
Dynamoid.configure do |config|
config.endpoint = 'http://localhost:8000'
end
Dynamoid.logger.level = Logger::INFO
version: '2'
services:
dynamodb:
image: amazon/dynamodb-local
ports:
- 8000:8000
class Document
include Dynamoid::Document
table name: :documents, key: :identifier
end
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'dynamoid'
gem 'pry-byebug', platforms: :ruby
gem 'rake'
GEM
remote: https://rubygems.org/
specs:
activemodel (5.2.3)
activesupport (= 5.2.3)
activesupport (5.2.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
aws-eventstream (1.0.3)
aws-partitions (1.196.0)
aws-sdk-core (3.62.0)
aws-eventstream (~> 1.0, >= 1.0.2)
aws-partitions (~> 1.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-dynamodb (1.34.0)
aws-sdk-core (~> 3, >= 3.61.1)
aws-sigv4 (~> 1.1)
aws-sigv4 (1.1.0)
aws-eventstream (~> 1.0, >= 1.0.2)
byebug (11.0.1)
coderay (1.1.2)
concurrent-ruby (1.1.5)
dynamoid (3.2.0)
activemodel (>= 4)
aws-sdk-dynamodb (~> 1)
concurrent-ruby (>= 1.0)
null-logger
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jmespath (1.4.0)
method_source (0.9.2)
minitest (5.11.3)
null-logger (0.1.5)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.7.0)
byebug (~> 11.0)
pry (~> 0.10)
rake (12.3.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
dynamoid
pry-byebug
rake
BUNDLED WITH
2.0.2
require 'dynamoid'
require_relative 'config'
require_relative 'document'
def watchdog(name)
yield
rescue Exception => ex
puts "Thread name: \"#{name}\", Exception: \"#{ex}\""
end
def safe_thread(name, &block)
Thread.new do
Thread.current.name = name
watchdog(name, &block)
end
end
100.times do |i|
safe_thread(i.to_s) do
puts 'debug' # To unlock Ruby's GVL
Document.where(identifier: 'hoge').first
end
end
loop do
sleep 5
end
Rake::Task.define_task(:environment)
require 'dynamoid/tasks'
require_relative 'config'
require_relative 'document'
namespace :dynamoid do
desc 'Insert db seeds into DynamoDB'
task :seed do
begin
Document.find('hoge')
rescue Dynamoid::Errors::RecordNotFound
Document.create(identifier: 'hoge')
end
end
end
@tsub
Copy link
Author

tsub commented Aug 5, 2019

How to setup

$ git clone https://gist.github.com/72e60233ed82a8a453428ea7441e6017.git reproduce-dynamoid-not-thread-safe
$ cd reproduce-dynamoid-not-thread-safe
$ docker-compose up -d
$ bundle
$ bundle exec rake dynamoid:create_tables
$ bundle exec rake dynamoid:seed

How to reproduce errors

$ bundle exec ruby main.rb
...
Thread name: "20", Exception: "undefined method `query' for #<Dynamoid::AdapterPlugin::AwsSdkV3:0x00007fdaf8bf2b08>
Did you mean?  to_query"
...
Thread name: "44", Exception: "undefined method `[]' for nil:NilClass"
...

⚠️ Script does not stop automatically, please enter Ctrl-C.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment