Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[BUG] Aws::Record::ItemCollection intermittently returns nil/raises from to_a and each

オープン
#153 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
aws, ruby
領域
databases

調査の方向性

aws-record-2.14.0/lib/aws-record/record/item_collection.rb から始め、特に28行目の each を確認し、Model.build_query.complete! に続けて to_a または each を実行して失敗を再現します。コレクションがどのように Aws::PageableResponse を取得するのかを追跡し、報告された nil each_page の経路を調査します。成功した DynamoDB クエリを、nil の結果や例外なしに常に列挙できれば完了です。

索引モデルが issue の本文から書いたものです。

説明

feature-request p2 queued
Describe the feature

Please harden Aws::Record::ItemCollection so it always honors the Enumerable contract. Today, calling to_a or each on a collection returned by Model.build_query.complete! can either raise or return nil, even when DynamoDB succeeded. The library should guarantee that enumerating a query result never blows up unless the service call itself failed.

Use Case

Every app using aws-record expects query.complete! to behave like a normal enumerable. In production we see sporadic NoMethodError: undefined method 'each_page' for nil:NilClass and cases where collection.to_a literally returns nil. That breaks paging, query helpers, and forces us to wrap every call site in defensive code just to prevent random 500s.

Proposed Solution

Ensure ItemCollection#each always has a backing Aws::PageableResponse and never returns nil.
Concretely:

  • Don’t mutate @items out from under each; memoize the client response before iterating.
  • Guard items.each_page with a lazy initializer so it never operates on nil.
  • Consider adding internal retries if the SDK hasn’t populated the items accessor yet.

As an immediate workaround we had to wrap the result:

def safe_array(result)
Array(result.to_a)
rescue StandardError
result.each_with_object([]) { |item, memo| memo << item }
rescue StandardError
[]
end

…but that’s brittle and doesn’t belong in every consumer.

Other Information
  NoMethodError: undefined method `each_page' for nil:NilClass
    aws-record-2.14.0/lib/aws-record/record/item_collection.rb:28:in `each'
    (ruby) enumerable.rb:225:in `to_a'
  • Repro snippet:
  collection = MyModel.build_query
    .on_index(:class_gsi)
    .key_expr(':tenant_and_class = ?', 'tenant-1#MY_MODEL')
    .complete!

  collection.to_a # randomly raises or returns nil

If aws-record embraced ActiveModel::Attributes (per issue #152), the enumerable bug would be a lot less painful to work around because:

  • Every attribute read/write would flow through Rails’ attribute stack, so you could normalize/
    sanitize values before they touch Dynamo’s marshalers—no need for custom “safe_to_a” guards
    sprinkled across the codebase.
  • You’d get ActiveModel’s dirty tracking and coercion for free, which means your
    Aws::Record::ItemCollection could just emit proper ActiveModel instances; they’d behave
    identically to ActiveRecord rows when enumerated, making it easier for the SDK team to reuse
    proven enumerable implementations.
  • Upstream fixes to ActiveModel types (e.g., handling nil serialization, type-casting edge cases)
    would land automatically, shrinking the surface area where the Dynamo-specific enumerable can go
    off the rails.

So, implementing the AMS attribute layer first would not only unlock normalization, it would also
simplify the SDK internals enough that bugs like the broken enumerable become much easier (maybe
even unnecessary) to patch.

Acknowledgements
  • I may be able to implement this feature request
  • This feature might incur a breaking change
aws-sdk-ruby-record version used

Ruby 3.4.4, aws-record 2.14.0, aws-sdk-dynamodb 1.176.x.

主要言語
Ruby
スター
318
フォーク
44
平均マージ
3日 15時間
マージ済み PR(30日)
4

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

aws/aws-record-ruby のほかの issue

aws/aws-record-ruby の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。