Your implementation is still subject to both potential issues I mentioned.

Let's suppose M is the module you are removing from the cache in (b), and
let's suppose the code for M includes the following lines:

var b = require("./b.js");
var thing = { ... };

exports.getThing = function () { return thing; };

Now, when you remove M from the cache, and load M1 in its place, "./b.js"
is still cached, and M1 will get that instead of any new version you may
have provided, and the mainline code of b.js will not be re-run.

Also, anyone who called M.getThing() and held a reference to it will have a
different 'thing' than anyone who later calls M1.getThing(). (I believe
this will also prevent M from being collected, since it's still in use.)

Whether these issues are okay in the context of your framework, only you
can say. I guess as long as you, and everyone else who writes a module to
your interface, understands all of this, you may be okay, but I do think
it's fragile.

--
Martin Cooper


On Sat, Aug 17, 2013 at 9:35 AM, Tony Huang <[email protected]> wrote:

> @Martin
>
> I have noticed this risk. So:
>
> 1) This will be just a limited feature used in my framework, it might go
> open source, but I will never try to make this a part of node.js API. So
> the risk will  affect very little area.
>
> 2) I will implement it in this way:
>    a) Get the old exports object from the cache
>    b) Remove the old module from the cache
>    c) Use require() to load the new module to the cache
>    d) Use new module's code_changed function to convert module state
>    e) Delete all fields of the old exports object
>    f) Copy all fields of the new exports to the old exports object
>    g) Update the status field of old exports object to the status object
> created at step d
>    h) Update the new modules exports field to the reference to the old
> exports object, to make sure the exports object of the module is unique in
> the system
>
> Thank you for your reply and expecting more comments & advices.
>
>
> On Sat, Aug 17, 2013 at 11:55 PM, Martin Cooper <[email protected]>wrote:
>
>> There are a couple of issues with that.
>>
>> First, if 'mod' loaded other modules to do its job, those other modules
>> are not removed from the cache. For example, suppose package 'foo' has main
>> module a.js, and that loads module b.js to do some of its work. Now if you
>> delete 'foo', or even a.js, from the cache, b.js is still cached, so when a
>> new version of a.js is loaded, it will still use that old b.js. Hence for
>> this to work reliably, you need to know all of the implementation files
>> that will need to be removed from the cache, not just your entry point.
>> That's pretty fragile.
>>
>> Also, depending on the nature of the modules being removed, and their
>> usage patterns, you may end up with retained references to the now-removed
>> (but possibly still extant) module, or objects created by it, that may
>> result in weird behaviour later. In the OP's case, with a framework
>> designed for this, that may not be so much of an issue, but it's still a
>> risk, especially if the modules being removed from the cache are
>> non-trivial and / or not written by the framework designer.
>>
>> --
>> Martin Cooper
>>
>>
>> On Sat, Aug 17, 2013 at 2:42 AM, George Stagas <[email protected]> wrote:
>>
>>> function removeCached(mod) {
>>>   delete require.cache[require.resolve(mod)];
>>> }
>>>
>>> // then
>>>
>>> require('foo');
>>>
>>> // ... later
>>>
>>> removeCached('foo');
>>>
>>>
>>>
>>> 2013/8/17 Tony Huang <[email protected]>
>>>
>>>> Hi all,
>>>>
>>>> I'm creating a framework for my game server, and developing the module
>>>> hot update feature.
>>>>
>>>> My design is very similar to the Erlang hot update feature. Modules
>>>> will be hot updated should meet the interface:
>>>> module.exports = {
>>>>   'status': "data",
>>>>   'version': "1.0",
>>>>   'init': function() {
>>>>      return initialStatus;
>>>>   },
>>>>   'code_changed': function(oldVersion, newVersion, oldStatus) {
>>>>      return newStatus;
>>>>   }
>>>> };
>>>>
>>>> So, when the module source file changes, the framework will get the
>>>> status of original version of module, and remove it from the module cache,
>>>> and import the new module, and invoke the code_changed method of the new
>>>> version of module to generate the new status and assign it to the new
>>>> module to finish the update process.
>>>>
>>>> As a result, I need an API to remove the module from the module cache,
>>>> will it be possible or  will this API be added in the future version of
>>>> node?
>>>>
>>>> Best regards.
>>>>
>>>> --
>>>> ------------------------------------------------------
>>>> Tony Huang    [email protected]
>>>>                      [email protected]
>>>>                      [email protected]
>>>>
>>>> --
>>>> --
>>>> Job Board: http://jobs.nodejs.org/
>>>> Posting guidelines:
>>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>>> You received this message because you are subscribed to the Google
>>>> Groups "nodejs" group.
>>>> To post to this group, send email to [email protected]
>>>> To unsubscribe from this group, send email to
>>>> [email protected]
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>>>
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "nodejs" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>
>>>  --
>>> --
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines:
>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To post to this group, send email to [email protected]
>>> To unsubscribe from this group, send email to
>>> [email protected]
>>> For more options, visit this group at
>>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>  --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> --
> ------------------------------------------------------
> Tony Huang    [email protected]
>                      [email protected]
>                      [email protected]
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to