Skip to content
This repository

Spira.types in threads #41

Open
wants to merge 1 commit into from

2 participants

Slava Kravchenko morrisjones
Slava Kravchenko

Hi! Here's another fix (with tests), this time addressing the use of Spira in "threaded environments".

A common use case would be a Rails app, where a client is given a dedicated thread upon connection. Since:
a) Spira uses Thread.current to access "types" (a hash which is populated only once when spira/types/* are declared, and
b) Spira classes are not reloaded during the "grand class reloading of Rails" (e.g. in development mode, where class caching is "off"),
Spira.types fails to get populated, and model classes (that are reloaded by Rails) with property declarations like "property :given_name, :predicate => FOAF.givenName, :type => String" cause a TypeError exception (":type => String" initiates a search for the type in now empty "Spira.types").

Generally, I don't understand the rationale behind having spira types stored in a "thread-local hash". Is there a case when the types are different between threads?

Potentially, my fix is thread-unsafe, but as long as Spira.types (or rather, underlying @types) is not modified at the run time (which is the case with Rails and, I suppose, with other sane apps ;-) ), it should be safe enough.

OT: I couldn't find a better way to send separate pull requests and I didn't feel it would've been a good idea to bundle this one with my previous pull request. So while it is marked as to be applied the previous bundle from me, it can be cleanly applied to the current upstream. Please let me know if it doesn't work.

morrisjones

Cordawyn, I think this problem has hit me big time, and is the gist of the issue I wrote here on StackOverflow '“Unrecognized type” error in haml view, but not console' http://stackoverflow.com/questions/8371181/unrecognized-type-error-in-haml-view-but-not-console

I'm just dipping into this now, trying to figure out a workaround without patching the Spira library directly.

Slava Kravchenko

Morrisjones, you're welcome to try my fork of spira (https://github.com/cordawyn/spira) and see if it works for you. You can try either "master" branch, containing only my fixes, or "bundled" branch, containing combined fixes from all around github (including mine).

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

Showing 1 unique commit by 1 author.

Sep 30, 2011
Slava Kravchenko making sure that Spira.types are available in threads 77f758b
This page is out of date. Refresh to see the latest.
2  lib/spira.rb
@@ -28,7 +28,7 @@ def repositories
28 28
   # @see Spira::Types
29 29
   # @return [Hash{Symbol => Spira::Type}]
30 30
   def types
31  
-    settings[:types] ||= {}
  31
+    @types ||= {}
32 32
   end
33 33
   module_function :types
34 34
 
27  spec/property_types.spec
@@ -7,6 +7,33 @@ describe 'types for properties' do
7 7
 
8 8
 
9 9
   context "when declaring type classes" do
  10
+    context "in a separate thread" do
  11
+      it "should be available" do
  12
+        types = {}
  13
+        t = Thread.new { types = Spira.types }
  14
+        t.join
  15
+
  16
+        types.should satisfy do |ts|
  17
+          ts.any? && ts == Spira.types
  18
+        end
  19
+      end
  20
+
  21
+      it "should be declared" do
  22
+        lambda {
  23
+          t = Thread.new do
  24
+            class ::PropTypeA
  25
+              include Spira::Resource
  26
+              default_vocabulary RDF::URI.new('http://example.org/vocab')
  27
+              base_uri RDF::URI.new('http://example.org/props')
  28
+
  29
+              property :test, :type => XSD.string
  30
+            end
  31
+          end
  32
+          t.join
  33
+        }.should_not raise_error TypeError
  34
+      end
  35
+    end
  36
+
10 37
     it "should raise a type error to use a type that has not been declared" do
11 38
       lambda {
12 39
         class ::PropTypeA
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.