einhorn 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,7 +49,7 @@ module Einhorn::Command
49
49
  # Need to avoid time-of-check to time-of-use bugs in blowing
50
50
  # away and recreating the old socketfile.
51
51
  destroy_old_command_socket(path)
52
- UNIXServer.new(path)
52
+ Einhorn::Compat.unixserver_new(path)
53
53
  end
54
54
  end
55
55
 
@@ -181,7 +181,8 @@ module Einhorn
181
181
  $stdout.flush
182
182
 
183
183
  # Spawn a child to pass the state through the pipe
184
- read, write = IO.pipe
184
+ read, write = Einhorn::Compat.pipe
185
+
185
186
  fork do
186
187
  Einhorn::TransientState.whatami = :state_passer
187
188
  Einhorn::State.generation += 1
@@ -190,7 +191,12 @@ module Einhorn
190
191
  }
191
192
  read.close
192
193
 
193
- write.write(YAML.dump(dumpable_state))
194
+ begin
195
+ write.write(YAML.dump(dumpable_state))
196
+ rescue Errno::EPIPE => e
197
+ e.message << " (state worker could not write state, which likely means the parent process has died)"
198
+ raise e
199
+ end
194
200
  write.close
195
201
 
196
202
  exit(0)
@@ -202,7 +208,11 @@ module Einhorn
202
208
  ENV.update(Einhorn::TransientState.environ)
203
209
 
204
210
  begin
205
- exec [Einhorn::TransientState.script_name, Einhorn::TransientState.script_name], *(['--with-state-fd', read.fileno.to_s, '--'] + Einhorn::State.cmd)
211
+ Einhorn::Compat.exec(
212
+ Einhorn::TransientState.script_name,
213
+ ['--with-state-fd', read.fileno.to_s, '--'] + Einhorn::State.cmd,
214
+ :close_others => false
215
+ )
206
216
  rescue SystemCallError => e
207
217
  Einhorn.log_error("Could not reload! Attempting to continue. Error was: #{e}")
208
218
  Einhorn::State.reloading_for_preload_upgrade = false
@@ -235,10 +245,12 @@ module Einhorn
235
245
  # have to track and manually close FDs for other cases, we
236
246
  # may as well just reuse close_all rather than also set
237
247
  # cloexec on everything.
248
+ #
249
+ # Note that Ruby 1.9's close_others option is useful here.
238
250
  Einhorn::Event.close_all_for_worker
239
251
 
240
252
  prepare_child_environment
241
- exec [cmd[0], cmd[0]], *cmd[1..-1]
253
+ Einhorn::Compat.exec(cmd[0], cmd[1..-1], :close_others => true)
242
254
  end
243
255
  end
244
256
 
@@ -0,0 +1,48 @@
1
+ module Einhorn
2
+ module Compat
3
+ # In Ruby 2.1.0 (and possibly earlier), IO.pipe sets cloexec on
4
+ # the descriptors.
5
+ def self.pipe
6
+ readable, writeable = IO.pipe
7
+ cloexec!(readable, false)
8
+ cloexec!(writeable, false)
9
+ [readable, writeable]
10
+ end
11
+
12
+ def self.cloexec!(fd, enable)
13
+ original = fd.fcntl(Fcntl::F_GETFD)
14
+ if enable
15
+ new = original | Fcntl::FD_CLOEXEC
16
+ else
17
+ new = original & (-Fcntl::FD_CLOEXEC-1)
18
+ end
19
+ fd.fcntl(Fcntl::F_SETFD, new)
20
+ end
21
+
22
+ def self.cloexec?(fd)
23
+ fd.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
24
+ end
25
+
26
+ # Opts are ignored in Ruby 1.8
27
+ def self.exec(script, args, opts={})
28
+ cmd = [script, script]
29
+ begin
30
+ Kernel.exec(cmd, *(args + [opts]))
31
+ rescue TypeError
32
+ Kernel.exec(cmd, *args)
33
+ end
34
+ end
35
+
36
+ def self.unixserver_new(path)
37
+ server = UNIXServer.new(path)
38
+ cloexec!(server, false)
39
+ server
40
+ end
41
+
42
+ def self.accept_nonblock(server)
43
+ sock = server.accept_nonblock
44
+ cloexec!(sock, false)
45
+ sock
46
+ end
47
+ end
48
+ end
@@ -18,7 +18,7 @@ module Einhorn::Event
18
18
  begin
19
19
  while true
20
20
  return if @closed
21
- sock = @server.accept_nonblock
21
+ sock = Einhorn::Compat.accept_nonblock(@server)
22
22
  Connection.open(sock)
23
23
  end
24
24
  rescue Errno::EAGAIN
data/lib/einhorn/event.rb CHANGED
@@ -10,17 +10,14 @@ module Einhorn
10
10
  @@connections = {}
11
11
  @@timers = {}
12
12
 
13
- def self.cloexec!(fd)
14
- fd.fcntl(Fcntl::F_SETFD, fd.fcntl(Fcntl::F_GETFD) | Fcntl::FD_CLOEXEC)
15
- end
16
-
17
13
  def self.init
18
- readable, writeable = IO.pipe
14
+ readable, writeable = Einhorn::Compat.pipe
15
+
19
16
  @@loopbreak_reader = LoopBreaker.open(readable)
20
17
  @@loopbreak_writer = writeable
21
18
 
22
- cloexec!(readable)
23
- cloexec!(writeable)
19
+ Einhorn::Compat.cloexec!(readable, true)
20
+ Einhorn::Compat.cloexec!(writeable, true)
24
21
  end
25
22
 
26
23
  def self.close_all
@@ -1,3 +1,3 @@
1
1
  module Einhorn
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
data/lib/einhorn.rb CHANGED
@@ -373,6 +373,7 @@ module Einhorn
373
373
  end
374
374
 
375
375
  require 'einhorn/command'
376
+ require 'einhorn/compat'
376
377
  require 'einhorn/client'
377
378
  require 'einhorn/event'
378
379
  require 'einhorn/worker'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: einhorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-16 00:00:00.000000000 Z
12
+ date: 2013-12-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -155,6 +155,7 @@ files:
155
155
  - lib/einhorn/client.rb
156
156
  - lib/einhorn/command.rb
157
157
  - lib/einhorn/command/interface.rb
158
+ - lib/einhorn/compat.rb
158
159
  - lib/einhorn/event.rb
159
160
  - lib/einhorn/event/abstract_text_descriptor.rb
160
161
  - lib/einhorn/event/ack_timer.rb
@@ -195,18 +196,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
195
196
  - - ! '>='
196
197
  - !ruby/object:Gem::Version
197
198
  version: '0'
198
- segments:
199
- - 0
200
- hash: -4058700174817085581
201
199
  required_rubygems_version: !ruby/object:Gem::Requirement
202
200
  none: false
203
201
  requirements:
204
202
  - - ! '>='
205
203
  - !ruby/object:Gem::Version
206
204
  version: '0'
207
- segments:
208
- - 0
209
- hash: -4058700174817085581
210
205
  requirements: []
211
206
  rubyforge_project:
212
207
  rubygems_version: 1.8.23
@@ -221,3 +216,4 @@ test_files:
221
216
  - test/unit/einhorn/command/interface.rb
222
217
  - test/unit/einhorn/event.rb
223
218
  - test/unit/einhorn/worker_pool.rb
219
+ has_rdoc: