# Random exception driving me crazy: SecurityError (Insecure: can't modify array)

**URL:** <https://www.ruby-forum.com/t/random-exception-driving-me-crazy-securityerror-insecure-cant-modify-array/130423>\
**Category:** Rails\
**Created:** [February 28, 2008, 3:54am UTC](https://www.ruby-forum.com/t/random-exception-driving-me-crazy-securityerror-insecure-cant-modify-array/130423 "2008-02-28T03:54:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [February 28, 2008, 3:54am UTC](https://www.ruby-forum.com/t/random-exception-driving-me-crazy-securityerror-insecure-cant-modify-array/130423/1 "2008-02-28T03:54:08Z")

</div>

Hi all, I’m running into a very strange but incredibly annoying  
problem that I just can’t seem to solve.

The exception is occurring from a seemingly innocuous line in my code,  
a simple ‘logger.warn(“some debug information”)’. The backtrace  
follows:

SecurityError (Insecure: can’t modify array)  
[RAILS\_ROOT]/vendor/rails/activesupport/lib/active\_support/buffered\_logger.rb:68:in  
`<<' [RAILS_ROOT]/vendor/rails/activesupport/lib/active_support/buffered_logger.rb:68:in`add’  
[RAILS\_ROOT]/vendor/rails/activesupport/lib/active\_support/buffered\_logger.rb:75:in  
`warn' [RAILS_ROOT]/app/models/order.rb:244:in`process\_with\_active\_merchant’  
[RAILS\_ROOT]/vendor/plugins/acts\_as\_state\_machine/lib/acts\_as\_state\_machine.rb:162:in  
`call’

The line in my model class, order.rb, that causes the exception is the  
following:

logger.warn(“Processing order with payment gateway”)

Now, I could probably just remove the offending log line to fix the  
problem, but this bothers me, since it may point to a larger problem  
that I’m not aware of, which I 'd rather not ignore. What makes this  
problem even more difficult to solve is that it doesn’t happen  
consistently and is very difficult to reproduce. It seems to only  
happen after my mongrel instances have been running for a few hours  
and are each using over 100 megs of memory (maybe there’s some memory  
leak or thrashing or something that’s causing this to happen). I’ve  
googled for the error and can’t seem to find much information, nor why  
it would be thrown from a Rails standard library file. I’ve tested  
this both on edge Rails 8117 and also a more recent edge Rails 8927  
with no difference.

When I restart my mongrel cluster, everything works fine, orders are  
processed, no exceptions are thrown. But after a few hours, maybe a  
day of running on the production site, I start getting the  
‘SecurityError (Insecure: can’t modify array)’ message as described  
above (it happens at the same place each time)

My next guess is that it could possibly have something to do with  
using acts\_as\_state\_machine, which I’m going to remove and see if it  
changes anything.

If anyone has any suggestions or advice, please let me know. I’ve  
been trying to fix this for over a week and haven’t made any progress.

For those interested, the following is the code that leads up to the  
error. It’s pretty simple stuff, we attempt to save the order, and in  
an after\_create method in the Order model, we charge the user’s credit  
card (using a transition from acts\_as\_state\_machine). If we can  
charge it successfully, we save the order, otherwise, we raise an  
exception and display an error message to the user.

orders\_controller.rb

def create

# populate order details (ommitted for brevity)

begin  
if @order.save  
# display successful message, redirect somewhere  
else  
# user probably forgot to enter some info, display error message  
end  
rescue  
# if we get here, then an exception must have been raised while  
# processing the credit card with active merchant  
logger.warn(“Error occurred while attempting to save order”)  
# render some page with warning message  
end  
end

order.rb

# we use acts\_as\_state\_machine to provide order state

acts\_as\_state\_machine :initial =\> :pending, :column =\> :status

# order has been created, but no payment has been received

state :pending

# process the payment with active\_merchant

state :processing\_payment, :after =\> :process\_with\_active\_merchant

# charge the user’s credit card

event :process\_payment do  
transitions :from =\> :pending, :to =\> :processing\_payment  
end

def after\_create

# the following method raises an exception if we fail to charge the

users

# credit card, prevents the order from being saved.

self.process\_payment!

# some other post-processing tasks

self.send\_order\_to\_distributor!

self.complete!  
end

def process\_with\_active\_merchant

# the following line causes a SecurityError (Insecure: can’t modify

array) exception  
logger.warn(“Processing order with payment gateway”)

gateway = ActiveMerchant::Billing::AuthorizedNetGateway.new({:login  
=\> API\_LOGIN\_ID, :password =\> TRANSACTION\_KEY})

response = gateway.purchase(self.total\_price, @credit\_card, options)

if response.success?  
logger.warn(“Successfully charged credit card with  
#{self.total\_price}: #{response.message}”)  
self.transaction\_id = response.authorization  
self.save  
else  
logger.warn(“Failed to charge credit card”)  
raise  
OrderFailedException.new(response.params[‘response\_reason\_code’]),  
response.message  
end

end

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [July 21, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/random-exception-driving-me-crazy-securityerror-insecure-cant-modify-array/130423/2 "2008-07-21T17:22:28Z")

</div>

Hey Mike,

Any update on this? I’m seeing something similar as well with  
acts\_as\_state\_machine

?

Mike G. wrote:

> Hi all, I’m running into a very strange but incredibly annoying  
> problem that I just can’t seem to solve.
> 
> The exception is occurring from a seemingly innocuous line in my code,  
> a simple ‘logger.warn(“some debug information”)’. The backtrace  
> follows:

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [January 24, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/random-exception-driving-me-crazy-securityerror-insecure-cant-modify-array/130423/3 "2009-01-24T12:18:21Z")

</div>

On Wed, Feb 27, 2008 at 9:53 PM, Mike G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> [RAILS_ROOT]/vendor/rails/activesupport/lib/active_support/buffered_logger.rb:68:in
> 
> ```
> 
> logger.warn(“Processing order with payment gateway”)
> 
> Now, I could probably just remove the offending log line to fix the  
> problem, but this bothers me, since it may point to a larger problem  
> that I’m not aware of, which I 'd rather not ignore. What makes this  
> problem even more difficult to solve is that it doesn’t happen  
> consistently and is very difficult to reproduce.

Rehashing an old message. I found a solution to this a while ago but  
never responded to my original message. Well, better late than never!  
Hopefully this will help someone in the future from wasting the  
amount of time that I did.

The problem turned out to be a bug with using self.method(action).call  
in acts\_as\_state\_machine. To fix it, I removed  
self.method(action).call from run\_transition\_action(action) method in  
acts\_as\_state\_machine.rb, and replaced it with self.send(action),  
since self.method(action) was causing the SecurityError (Insecure:  
can’t modify array).

Here’s two other links from people who have run into the same problem  
in the past:

[http://cmms.norfello.com/cmms/ticket/445](http://cmms.norfello.com/cmms/ticket/445)  
[http://code.google.com/p/dry-scaffold/source/browse/dry\_scaffold/CHANGELOG](http://code.google.com/p/dry-scaffold/source/browse/dry_scaffold/CHANGELOG)

This issue existed in revision 66 of the acts\_as\_state\_machine from  
here:

[http://elitists.textdriven.com/svn/plugins/acts\_as\_state\_machine/trunk/lib/acts\_as\_state\_machine.rb](http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/lib/acts_as_state_machine.rb)

I’m not sure if it still exists in the github version.

Mike
